I am using the code-igniter framework and would like to pull data from an external servers api using PHP Curl. I haven't used this before so getting a little stuck and my research is coming up empty.
I have this working in google postman but just need to replicate this in PHP.
google postman config:

My Curl syntax:
$url = "https://api.doamin.com/v1/config/limits";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Connection: Keep-Alive',
'account:A004',
'key : 1234-12'
));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$request= curl_getinfo($ch);
var_dump($request);
echo "<br><br><br>Reply:";
$content = curl_exec($ch);
I have got this working to an extent as I now get a response from my server however the headers I am sending in my syntax are not being applied. the page output is as follows:
array(22) { ["url"]=> string(94) "https://api.domain.com/v1/config/limits" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(0) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0) ["namelookup_time"]=> float(0) ["connect_time"]=> float(0) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) ["certinfo"]=> array(0) { } ["redirect_url"]=> string(0) "" }
Reply:HTTP/1.1 401 Unauthorized Server: nginx Date: Thu, 03 Dec 2015 14:46:26 GMT Content-Type: application/json; charset=utf-8 Content-Length: 51 Connection: keep-alive Access-Control-Allow-Origin: * {"status":"failure ","data":"Authentication Error"}
Why are my headers not being shown and where they are like content type it is null rather than application/json as being set in the code.
Thanks as always for the help.
if Curl is not the best option to use to access this api from PHP then please advise as well. Thanks
Comment user_agent parameter or use this config
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_HEADER, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
//curl_setopt($ch, CURLOPT_PROXY, PROXY);
$result = curl_exec($ch);
$error = curl_error($ch);
if ($error) echo $error;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With