Error with file_get_contents() [duplicate] - php

How can I read a response from Stackoverflow API in PHP? The response is GZIP-ed. I found e.g. the following suggestion:
$url = "http://api.stackoverflow.com/1.1/questions/" . $question_id;
$data = file_get_contents($url);
$data = http_inflate($data);
but the function http_inflate() is not available on the installation that I am using.
Are there some other easy ways to accomplish it?

A cool way
http://www.php.net/manual/en/wrappers.compression.php
Notice the use of a stream wrapper, compress.zlib
$url = "compress.zlib://http://api.stackoverflow.com/1.1/questions/" . $question_id;
echo $data = file_get_contents($url, false, stream_context_create(array('http'=>array('header'=>"Accept-Encoding: gzip\r\n"))));
or using curl
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url
, CURLOPT_HEADER => 0
, CURLOPT_RETURNTRANSFER => 1
, CURLOPT_ENCODING => 'gzip'
));
echo curl_exec($ch);
edited--
other methods removed because they don't send an Accept-Encoding http header.

Use this to get gzip encoding json response its working like a charm
function get_gzip_json($url) {
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_ENCODING => 'gzip',
));
$json = curl_exec($ch);
$json= mb_convert_encoding($json, "UTF-8", "UTF-8");
return $json;
}

Related

Submitting with PHP and Curl

I've been trying all morning to get an XML string uploaded via an API so that it submits my order but no matter what I try it simply isn't working for me.
My URL:
$url = "http://example.com/SubmitOrder?apiKey=ABC123&clientID=MYId&orderXml=".$xml;
$xml is my xml details already pre-formatted.
I then put this into my curl section:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url
));
$resp = curl_exec($curl);
$xml1=simplexml_load_string($resp) or die("Error: Cannot create object");
print_r($xml1);
echo "Submitted";
The response I get is "Error: Cannot create object" and I can see that my details have not been submitted.
Where am iI going wrong ??
Many thanks.
You can check your response data and create data in this code example.
<?php
$url = 'https://www.w3schools.com/xml/note.xml';
$resp = file_get_contents($url);
if (resp) {
$string = simplexml_load_string($resp);
var_dump($movies);
}
but if you want get data in method curl try this.
<?php
$html_brand = 'https://www.w3schools.com/xml/note.xml';
$ch = curl_init();
$options = array(
CURLOPT_URL => $html_brand,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
);
curl_setopt_array( $ch, $options );
$response = curl_exec($ch);
switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
case 200: # OK
break;
default:
echo 'Status code HTTP: ', $http_code, "\n";
}
var_dump($response);
curl_close($ch);
die();
Check your data. And check your response status code.

Curl - Unreadable data

I'm using curl in PHP to fetch an image from another website and the output is unreadable. It's like +�<�#�9�3�*zB�<�4�<�I�;�4�2�;�3xO�. I'm using php7 on XAMPP.
$ch = curl_init();
$option = array(
CURLOPT_USERAGENT => $user_agent,
CURLOPT_ENCODING => '',
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true
);
curl_setopt_array($ch, $option);
$response = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close($ch);
print_r($response);
header('Content-type: image/jpeg');

PHP Curl does not send POST request to server

I have this code:
<?php
$data = array('name' => 'Ross', 'php_master' => true);
$url = 'http://dsaasd.adsds.nl:8081/cops.nsf/orders?openagent';
$html_brand = "www.google.com";
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
CURLOPT_VERBOSE => true,
);
curl_setopt_array( $ch, $options );
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ( $httpCode != 200 ){
echo "Return code is {$httpCode} \n"
.curl_error($ch);
} else {
echo "<pre>".htmlspecialchars($response)."</pre>";
}
curl_close($ch);
?>
The problem is, if i send curl post to:
$url = 'http://dsaasd.adsds.nl:8081/cops.nsf/orders?openagent';
Then it does NOT work.
But if i send curl post to the same service but on a different server then it works, this is the other server
$url = 'http://dsaasd.adsds.nl:80/cops.nsf/orders?openagent';
I also post data by normal form post to:
$url = 'http://dsaasd.adsds.nl:8081/cops.nsf/orders?openagent';
And then it works and i receive data on the server.
But with this curl post i keep getting:
Return code is 0 Failed to connect to 11.43.45.123: Network is unreachable
Anyone have any idea?
I think you need to add CURLOPT_POST to the $options array. Docs here.

PHP cURL to many urls

I have php a code and can't understand why the php script creates only 4 files, but without the data. If I use curl only for one url without foreach loop all will be ok. Any thoughts?
$years = array('2012', '2013', '2014', '2015');
foreach($years as $year)
{
$url = "http://lpo.dt.navy.mil/data/DM/Environmental_Data_Deep_Moor_{$year}.txt";
$fp = fopen(base_path() . "/database/rawdata/{$year}.txt", 'w');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_FILE => $fp,
CURLOPT_VERBOSE => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FAILONERROR => 1
));
curl_exec($curl);
if (!curl_errno($curl))
{
$info = curl_getinfo($curl);
Log::info("File created {$year}.txt");
Log::notice("Evaluation of this script was {$info['total_time']} seconds!");
}
else
{
Log::error("cURL ERROR " . curl_error($curl));
}
curl_close($curl);
fclose($fp);
}
See joeterranova's comment on the PHP site:
It appears that setting CURLOPT_FILE before setting
CURLOPT_RETURNTRANSFER doesn't work, presumably because
CURLOPT_FILE depends on CURLOPT_RETURNTRANSFER being set
.
Therefore you should change your curl_setopt_array to the following:
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FILE => $fp,
CURLOPT_VERBOSE => 1,
CURLOPT_FAILONERROR => 1
));

Send a data request with headers

I'm using Twitter API v1.1. I've created a valid authorization header (using 0auth).
Now I want to actually send a request to Twitter for the data I want but I'm fairly new to PHP and certainly haven't got a damn clue about cURL.
So far I've got:
$authHeader = 'Authorization: 0Auth ....... Expect:'
$baseURL = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name='.$screenName.'&count='.$tweetCount;
Then I found the following code in twitterAPIexchange which I can't get working for me:
$options = array(
CURLOPT_HTTPHEADER => $authHeader,
CURLOPT_HEADER => false,
CURLOPT_URL => $baseURL,
CURLOPT_RETURNTRANSFER => true
);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
Can anyone help me with the header formation to make this request?
You must pass the headers as an array, e.g.
curl_setopt($feed, CURLOPT_HTTPHEADER, array('HeaderName1: HeaderValue1', 'HeaderName2: HeaderValue2'));
Or, in your case,
$authHeader = array('Authorization: OAuth oauth_consumer_key...');
$baseURL = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name='.$screenName.'&count='.$tweetCount;
$options = array(
CURLOPT_HTTPHEADER => $authHeader,
CURLOPT_HEADER => false,
CURLOPT_URL => $baseURL,
CURLOPT_RETURNTRANSFER => true
);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);

Categories