0

I trying to convert the cURL code found here in the first example code. Here is the cURL they give me:

curl -X GET -H "Content-type: application/json" -d '{"api_key":"zUYwqLqwTqpyJ2bA7osy"}' https://statushub.io/api/status_pages

Here is what I have so far, and it isn't working:

<?php
#$cmd= 'curl -X GET -H "Content-type: application/json" -d \'{"api_key":"a991d51f4f7ea269ccfbfa68621b3aa3"}\' https://statushub.io/api/status_pages';
#exec($cmd,$result);
#echo gettype($result), "\n";
#echo print_r(curl_exec($cmd));

$ch = curl_init('https://statushub.io/api/status_pages');

curl_setopt($ch, CURLOPT_HTTPHEADER, 'Content-type: application/json');
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"api_key":"a991d51f4f7ea269ccfbfa68621b3aa3"}');
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$test = curl_exec($ch);
curl_close($ch);

echo (string)$test;

if(curl_exec($ch) === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
else
{
    echo 'Operation completed without any errors';
}

echo "<pre>";
print_r($test);
echo "</pre>";
?>

I included some other attempts with lines coded out. I have tried looking at the php cURL documentation, but I can't figured this one out.

Thanks in advance.

Loaf
  • 3,011
  • 2
  • 15
  • 25

1 Answers1

1

The following is a direct translation of the curl command, the only major difference I notice is that the headers are in an array and setting the CURLOPT_CUSTOMREQUEST to GET:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://statushub.io/api/status_pages");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"api_key":"zUYwqLqwTqpyJ2bA7osy"}');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$test = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

echo "<pre>";
print_r($test);
echo "</pre>";
John C
  • 8,223
  • 2
  • 36
  • 47
  • Thanks, that got me a response actually. However it says "Error:Failed connect to statushub.io:443; No route to host". I updated the api key since I have a different one. If I run it in command cURL i get the correct response back. Any ideas? – Loaf Feb 18 '16 at 09:58
  • I've tried setting verify pere and host to false/0 but that didn't work. I also read that it is bad security wise. I tried downling the cert and providing it via CAINFO, but the same error is appearing – Loaf Feb 18 '16 at 10:38
  • 1
    "No route to host" is a network connection error rather than an SSL one. If you can connect from the command line, it could be a proxy issue or something trickier. [This answer](http://stackoverflow.com/a/29783772/628267) suggests adding `curl_setopt( $curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);` to resolve IPv6 issues. – John C Feb 18 '16 at 12:33
  • Hmm, that was a no go as well. Either way, originally I needed it to be converted and that is what you did. Thanks again, marking as answer. – Loaf Feb 18 '16 at 12:56