Avatar of Neil_Bradley
Neil_Bradley
Flag for New Zealand asked on

Decode a json curl

Hi EE,
I have successfully grabbed json data from a URL. Code uses is
$username='###';
$password='###';
$url='###';

$ch = curl_init();
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
$result=curl_exec ($ch);
curl_close ($ch);

Open in new window

The output of the can be seen here Link to output
I now need to make scene of this data. How would I create an array from this information that I can echo with some basic formatting?
PHP

Avatar of undefined
Last Comment
Dave Baldwin

8/22/2022 - Mon
SOLUTION
Dave Baldwin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Neil_Bradley

ASKER
Great solutions Dave and Ray.
Dave, following on from your suggestion I have updated the code to this
$ch = curl_init();
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE  );
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
$result=curl_exec ($ch);
curl_close ($ch);

$obj = json_decode($result);
echo "<ul>";
foreach ($obj as $element)
{
    echo "<li><strong>".$element->Name."</strong>";
	 echo "<br>Location: ";
    echo $element->Location;
    echo "<br>and IS RATED ";
    echo $element->Stars;
    echo " STARS<br></li>";
	echo $element->Description;
}
echo "</ul>";

Open in new window

This works fine. Was this what you had in mind?
Ray, your solution works perfectly and I see a lot of benefit from initially creating a file to receive the data and grabbing this as and where needed. The next phase of this particular project is to cache the resulting data so I can limit calls to the external source. I may be using your expertise again to help with this question.
Cheer and thanks to both of you.
Neil
Dave Baldwin

Yep, that's it.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck