Curl - Unreadable data - php

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');

Related

PHP cURL Response Header

I'm trying out HTTP Requests for the first time (also using PostMan). There, everything works fine. When exporting it with PHP cURL I get the following:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "htt..",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic ..."
),
));
$response = curl_exec($curl);
curl_close($curl);
I am expecting a Key in the response Header, So I'm trying to get the Response Header as a variable (pref. Array). How would I do this? I got no luck so far trying to use the following:
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
Thanks for any help in advance.
Edit 1:
Using https://incarnate.github.io/curl-to-php/ I converted the Postman cURL command to PHP cURL (instead of using Postman's generator) and got the following, which seems to work:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'htt..');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //manually added
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //manually added
curl_setopt($ch, CURLOPT_HEADER, 1); //manually added
$headers = array();
$headers[] = 'Authorization: Basic ...';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
$body = substr($result, $header_size);
curl_close($ch);
echo $header;
Your'e really close to solving this, but you need to set the CURLOPT_HEADER to true to retrieve the headers. This change should work:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "htt..",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HEADER => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic ..."
),
));
$response = curl_exec($curl);
if (curl_errno($curl) !== CURLE_OK) {
throw new \RuntimeException("curl_exec error: " . curl_error($ch) . ": " . curl_error($ch));
}
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
curl_close($curl);
$response is always only response body.
Try to look at option for curl_getinfo
CURLINFO_HEADER_OUT - The request string sent. For this to work, add the CURLINFO_HEADER_OUT option to the handle by calling curl_setopt()
Here is the documentation https://www.php.net/manual/en/function.curl-getinfo.php

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.

How do I connect to the PredictIt API (type application/xml) using PHP/CURL?

This code is not returning anything:
<?php
$ch = curl_init();
$header = array('Contect-Type: application/xml', 'Accept: application/xml');
$options = array(
CURLOPT_URL => 'https://www.predictit.org/api/marketdata/ticker/RNOM16',
CURLOPT_HTTPHEADER => $header,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_RETURNTRANSFER => TRUE
);
curl_setopt_array($ch, $options);
$data = curl_exec($ch);
curl_close($ch);
echo '<p>Data</p>';
print_r($data);
?>
Does anyone know what's wrong?
Thank you.

Error with file_get_contents() [duplicate]

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;
}

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.

Categories