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.

1
  • You may need the ssl options and the return transfer likely Commented Feb 18, 2016 at 3:50

1 Answer 1

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>";
Sign up to request clarification or add additional context in comments.

4 Comments

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?
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
"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 suggests adding curl_setopt( $curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); to resolve IPv6 issues.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.