Reputation: 11
I need help getting specific data from an api. The json data looks something like this
array(1)
{
[0]=> string(18) "{"errorCode":0
"members":[
{"userId":"32"
"name":"Joy"
"age":"22"
"country":"USA"
"orders":133
"active":0}
{"userId":"38"
"name":"greg"
"age":"29"
"country":"CA"
"orders":19
"active":0}
{"userId":"29"
"name":"bob"
"age":"33"
"country":"USA"
"orders":67
"active":0}
],"total":59},
Array"
}
What I want to get back is only people in the US that are over 25.
My code so far is
<?PHP
$url="https://api.someplace.com";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, $url );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
$content = curl_exec( $ch );
$response = curl_getinfo( $ch );
$combine = "$content, $response";
curl_close ( $ch );
$openBracket = str_replace("{","{\n",$combine);
$comma = str_replace(",",",\n",$openBracket);
$result = str_replace( ',', '<br />', $comma );
$ary = array($result);
echo $ary;
?>
This returns the whole json file but when I try to do anything with the data it doesn't work. I know I need some kind of foreach loop, if/else statements and maybe a json_decode, I wont bore you with what I all tried so far because none of it worked, I only get returns of all the json data back, NULL, {, or just a blank page.
I'm not sure if cURL is messing things up, I never used it before, I have to with this one because it's a https link. Please let me know the best way to get this data.
Upvotes: 1
Views: 317
Reputation: 69967
For one, you're missing the all important cURL option CURLOPT_RETURNTRANSFER
that tells cURL to return the content when you call curl_exec
rather than output it to stdout.
Second, you're doing some strange manipulations that would totally break the JSON.
This should be sufficient:
<?php
$url = "https://api.someplace.com";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, $url );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec( $ch );
$response = curl_getinfo( $ch );
// $combine = "$content, $response"; // this is useless
curl_close ( $ch );
$response = json_decode($content, true);
var_dump($response); // decoded JSON, accessible as an array
Upvotes: 1
Reputation: 37
First of all, the content provided has invalid JSON data (as you can already tell by yourself). So you're going to have to parse it.
If you need help parsing let me know. It can easily be done with a regex expression.
Once you've converted the content into valid JSON you can convert it to an array using the json_decode function for easier accessibility.
Usage:
$data = $valid_json;
$array = json_decode($data, true);
The true
parameter will convert the result into an array instead of an object (stdClass).
Example (when compiling an array to JSON code):
$json_data = json_encode([
'apple_amount' => 14
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
You don't need to use JSON_PRETTY_PRINT
. It's used for returning a "prettier" result.
JSON_UNESCAPED_UNICODE
is used to preserve unicode characters.
Example (compiling JSON into an array):
$result = json_decode($json_data, true);
$apples = $result['apple_amount'];
I'm almost positive your issue is originating from invalid JSON and I'm not sure what you're trying to achieve here:
$openBracket = str_replace("{","{\n",$combine);
$comma = str_replace(",",",\n",$openBracket);
$result = str_replace( ',', PHP_EOL, $the );
Upvotes: 0