PHP cURL JSON Decode (X-AUTH Header)

Posted by TheCyX on Stack Overflow See other posts from Stack Overflow or by TheCyX
Published on 2014-05-29T09:21:24Z Indexed on 2014/05/29 9:24 UTC
Read the original article Hit count: 429

Filed under:
|
|
|
<?php

// Show Profile
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "https://example/api");
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
curl_setopt ($curl, CURLOPT_HTTPHEADER, array('X-AUTH: 123456789'));
$projects = curl_exec($curl);

// This is empty?
echo $projects;

//Decode
$phpArray = json_decode($projects);
print_r($phpArray);
foreach ($phpArray as $key => $value) { // Line 17, sure its empty, but why?
echo "<p>$key | $value</p>";
}

?>

Warning: Invalid argument supplied for foreach() in /html/api.php on line 17

The API needs this authentification:

$ curl -i -H "X-AUTH: 123456789" https://example/api

JSON File:

{"id":"123456","hostId":null,"Nickname":"thecyx","DisplayName":"thecyx","AppDisplayName":"thecyx","Score":"300","Account":"Full"}

The $project variable is empty. If I'm posting the API Url in the Broswer its working.

And, if possible, what's the correct way to get the JSON Data e.g. [Nickname],[Score]?

© Stack Overflow or respective owner

Related posts about php

Related posts about JSON