Parsing object in PHP not working - php

I'm using this to get my response (only relevant code below):
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
$my_resp = $result['total_count'];
var_dump($my_resp);
echo $result gives me an object that appears to be valid JSON. Like this:
{
"total_count": 1
}
However, $my_resp comes back as NULL. What am I doing wrong here?

You need to turn it into an object from a string
$result = json_decode( curl_exec($ch));
or an associative array
$result = json_decode( curl_exec($ch), true );

If it is JSON, you should json_decode() it before trying to read fields.

Related

JSON Encode To PHP Variable via Curl fails with empty response

i have searched a lot on google and here but im not getting any further with my problem. I am not a coder though I am trying to parse JSON to PHP Variables, but i get an empty response, where i want a table to be shown or at least any jsondata
Here is what my code looks like:
<!DOCTYPE html>
<html>
<body>
<h1>Available Agents </h1>
<?php
$url = 'https://url/livewebservice/imoscontactagentstate?Username=username&Pwd=password&Cmd=GetState&ResultType=JSON';
// Initiate curl
$ch = curl_init ($url);
$data = json_encode ($data,true);
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array (
' Content-Type: application/x-www-form-urlencoded ',
'charset=utf-8')
);
$result = curl_exec ($ch);
curl_close ($ch);
return $result;
var_dump(json_decode($result, true));
print_r($result);
foreach ($result as $key => $value)
{
echo ' <td><font face="calibri"color="red">'.$value[type].' </font></td><td><font face="calibri"color="blue">'.$value[category].' </font></td><td><font face="calibri"color="green">'.$value[amount].' </font></tr><tr>';
}
echo "</tr></table>";
?>
</body>
</html>
I am grateful for any hints
Try to remove true from $data = json_encode ($data,true); as far as can i remember true is used only in json_decode to create an associative array
The problem was solved my Username did not have the permission to access the data and we made minor changes to the code so it looks like this:
php
$data = array(
"UserName" => "Username",
"Pwd" => "Password",
"Cmd" => "GetAgentStateList",
"ResultType" => "JSON",
);
$url='https://host/livewebservice/service/?'.http_build_query($data);
echo $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/x-www-form-urlencoded", "charset=UTF-8",));
$result = curl_exec($ch);
if(curl_errno($ch)){
throw new Exception(curl_error($ch));
}
curl_close($ch);
$f_result=json_decode($result);
print_r($f_result);
?>
i hope this helps someone coming from google someday.

cURL PHP Getting value of JSON

Response:
{
"error":null,
"value1":"STRING",
"valuelist":{
valueurl:{
"status":STATUS_TEXT,
},
}
}
cURL:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $response);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
$content = curl_exec ($ch);
curl_close ($ch);
return $content;
now I have json_decoded it, I can't get value of "status".
echo $foo->value1; works fine.
echo $foo->status; isn't right. How would I get that value?
The value of status should be access like below:
$foo->valuelist->valueurl->status
When sometimes you don't know where is certain property or index you can always maka a var_dum() de variable, example:
var_dump(json_decode($content));
So you can see the tree structure:
$foo->valuelist->valueurl->status

How to get JSON parameter from URL

Hello I'm coming from Get JSON object from URL
I tried to get marketCap->usd.
Can anyone tell me what I'm doing wrong please?
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://coinmarketcap.northpole.ro/api/v5/ZCL.json');
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
echo $result->marketCap->usd;
Change echo $result->marketCap->usd; for echo $obj->marketCap->usd;
You have saved the data decoded in $obj.
Last line of your code should be
echo $obj->marketCap->usd;

PHP: How to Parse JSON From API call?

I have a CURL response that looks like this:
{"page_number":2,"items":[],"items_per_page":1,"kind":"search#companies","total_results":0,"start_index":1}
All i need from that is the value of "total_results":0
so I am trying to get "total_results" like this:
$url = "https://api.companieshouse.gov.uk/search/companies?q=POOdfgdfgyygfhfgfgfghgfP&items_per_page=1&start_index=0";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "my_password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$info = curl_getinfo($ch);
curl_close($ch);
echo $output->total_results[0];
However, when I echo the $output->total_results[0];, I get nothing being echoed on my page. So i don't really know what I am doing wrong!
Could someone please advise on this issue?
any help would be appreciated.
What you got as a response is a JSON string. You can parse it to a stdClass object using json_decode:
$object = json_decode( $output );
Then you can access the field you want:
$total_results = $object->total_results;
Alternatively, you can parse the JSON string to an array:
$array = json_decode( $output, true );
and get the var:
$total_results = $array['total_results'];

php youtube json decode

I tried to use JSON decode to get the youtube API feed. However, when I paste the JSON result in http://www.jsonlint.com/ I noticed something like
"media$group": {
"media$category": [
Unfortunately some symbols are rejected by php. Here is my code, I tried to remove this $ symbol, but maybe not success. How do I solve this?
$url = 'http://gdata.youtube.com/feeds/api/videosq=football&orderby=published&v=2&alt=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body1 = curl_exec($ch);
$body = str_replace('$','', $body1);
curl_close($ch);
$data = json_decode($body);
foreach ($data->feed->entry as $result) {
...
}
Your problem is the usage of PHP identifiers to access the contents. The simplest solution here would be to get an array instead of an object:
$data = json_decode ( $json , $assoc = true );
This allows access to fields with:
echo $result['media$group']['media$description'];
If you want to keep the object syntax, that's possible with this kludge:
echo $result->{'media$group'}->{'media$category'};
(But arrays are safer here. You don't get a fatal error should the format change and properties be absent.)
This work:
<?php
$url = 'http://gdata.youtube.com/feeds/api/videos?q=football&orderby=published&v=2&alt=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body1 = curl_exec($ch);
$body = str_replace('$','', $body1);
curl_close($ch);
$data = json_decode($body);
foreach ($data->feed->entry as $result) {
var_dump($result);
}
?>

Categories