This question already has answers here:
Parsing JSON in PHP "stdClass could not be converted to string" error
(4 answers)
Closed 8 years ago.
I keep getting this error when I parse the trip advisor feed. Trying to pull up the username.
Catchable fatal error: Object of class stdClass could not be converted to string
Here's my code with the link to the feed.
<?php
function getCode($url)
{
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$html=getCode("http://api.tripadvisor.com/api/partner/1.0/location/258705/reviews");
$json = json_decode($html);
$cnt=0;
foreach ($json as $item)
{
foreach($item as $row)
{
echo "Image Url:".$row->text."<br>";
echo "ID:".$row->id."<br>";
//username
foreach($row->user as $row1)
{
echo $row1."<br>";
}
}
echo "<br><br>";
}
?>
print_r($row1);
stdClass Object ( [id] => [name] => Mahwah )
Try changing
$row1."<br>";
to
$row1->name."<br>";
You need to set the assoc attribute to true on the json_decode function.
Try this:
$json = json_decode($html,true);
Read more about json_decode here
Also as a suggestion for your next stack overflow post, don't share API keys or other sensitive information.
Related
This question already has answers here:
Get JSON object from URL
(11 answers)
Closed 11 months ago.
i use a php post to get the number of users in the telegram group
<?php
$post = array(
'chat_id'=>$chat_id);
$ch = curl_init("https://api.telegram.org/bot$tokken/getChatMembersCount?");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING,"");
header('Content-Type: text/html');
$posres = curl_exec($ch);
$data1 = json_encode($posres);
echo $data1;
?>
the response are array :
{"ok":true,"result":3}
how can i access the result value i tried echo $data1[19]; this only give the index position value
thank you
The response is JSON, so you need to use json_decode() to convert it to a PHP array or object.
$data1 = json_decode($posres);
echo $data1->result;
Just in case Your response is array of object
echo $data1['19']->result; this should give 3 according to your question
i have this code, and i get the error when i use foreach on the bottom of the page.
<table>
<tr>
<?php
$data =('https://www.example.com/api/report_advertiser?key=abcd&campaigns=6757955,6781745,6739793,6349821&quick=last_month');
$data_json = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
foreach ($response as $value)
echo $value['campaigns']
?>
</tr>
</table>
$response = curl_exec($ch);
Looking at this code, you should know that curl_exec() returns a string, when the execution was successful, otherwise false.
But in the next step you iterate over a string
foreach ($response as $value) {
echo $value['campaigns'];
}
You can iterate only over iterable types like arrays.
As it looks like you expect a JSON string you can convert it into an array. Use json_decode() for that and pass true as second parameter to receive an associative array instead of an object.
Assuming you will receive a multidimensional array having the inner loops key campaigns, following code will do the job.
$response = json_decode($response, true);
foreach ($response as $value) {
echo $value['campaigns'];
}
Update 1
Using the URL you provided before (I anonymized because you submitted credentials) you will receive an error.
if (!empty($response['errors'])) {
throw new Exception(print_r($response['errors'], true));
}
Gives out following, so it can not work as you expect, because the data is simply not available.
[status] => 405
[code] => 13
[title] => Method not allowed for this action
This question already has answers here:
PHP CURL isn't processing encoded return data properly
(4 answers)
Can't get data from Stack Exchange API
(2 answers)
Closed 3 years ago.
I am using StackExchange get all answers API to my project.
so that I am hitting the URL https://api.stackexchange.com/2.2/answers?page=10&pagesize=10&order=desc&sort=activity&site=stackoverflow
While hitting in browser the original json shows, but the problem is using program.
PHP
<?php
//step1
$cSession = curl_init();
//step2
curl_setopt($cSession,CURLOPT_URL,"https://api.stackexchange.com/2.2/answers?page=10&pagesize=10&order=desc&sort=activity&site=stackoverflow");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
//step3
$result=curl_exec($cSession);
//step4
curl_close($cSession);
echo"<pre>";
var_dump($result);
echo "<br>";
echo"</pre>";
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($result, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);
foreach ($jsonIterator as $key => $val) {
if(is_array($val)) {
echo "$key:";
echo"<br>";
} else {
echo "$key => $val";
echo"<br>";
}
}
?>
string(1194) "���mO�8ǿJ���m���=!��n�e�ǞN��84���K)��~NZ�4
�t���d����?�����O1��]�����;�=����:P�>�+�3eOb�U�f���1�O���T�G�Ȫt�E�ɴ�5ɣ�X�$�$Yj�խ,�T���dK�i��M��+k�Ǯ��#��r����HKٵJ_毀�.Jm�:�M�|m�!u���k���˼�$�r�,:u�����ϩ,�矗c��t�|�2�1u�#��ɨ���l6�G��Zu�_m�Cz����{4\脄Oȃ����'����C�W�9��a��U�2Qv.�~C��淋ܩ����'�|a�.�/��ۼ���� �l
��u8".}��P�Y�e�he��
!a�`����<���>>������Aė!�[�;�E.�1�\�E ���rcv��7�#�4[�V��J����q�� �$�̇S���_W��d�t�'�W��'y�m����8��x�)���ӫ^q8&�#�F�6���X��+�6�����O2������{�g��I(�εR[Ld���7K�rߍzC8h#;�]��0�M�P1�~A��l�N�G^��2dBB#!��!$����3������I� )��
�-���n�םH�7H�
�g���y�"E���ۘ��2b��O�f�^}�u9tf�����!Ð�t�4}O
��MJ�[��iw�1n���uR��m=�6��<��Mw��]+�(��u��s��e��t>�~&�$7��FD�=��R�^�"
it shows the result like some encoded string, give the solutions for that. Thanks in advance.
Using 'gzdecode' function will decode from GZIP JSON
$ch = curl_init();
// Set the url
$url = "https://api.stackexchange.com/2.2/answers?page=10&pagesize=10&order=desc&sort=activity&site=stackoverflow";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
$result = gzdecode($result); // Here Decoding
print_r($result);
I received Out like below
{"items":[{"owner":{"reputation":1642,"user_id":6373435,"user_type":"registered", ... . . .
Try this it's working to me
check this URL with terminal it shows encode format:gzip.
so that we need to encode that with the following line.
curl_setopt($cSession, CURLOPT_ENCODING, 'gzip');
Full code=>
$cSession = curl_init();
//step2
curl_setopt($cSession,CURLOPT_URL,"https://api.stackexchange.com/2.2/answers?page=10&pagesize=10&order=desc&sort=activity&site=stackoverflow");
curl_setopt($cSession, CURLOPT_ENCODING, 'gzip');//--------->>> Solution step
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
//curl_setopt($cSession,CURLOPT_HEADER, false);
//step3
$result=curl_exec($cSession);
//step4
curl_close($cSession);
echo"<pre>";
var_dump($result);
echo "<br>";
echo"</pre>";
Thanks - Udayasankar.
Basically I'm trying to GET an API that gives me a JSON array. It should only be one integer. Whenever I try to though I receive the error:
Notice: Trying to get property of non-object in /public_html/call.php on line 16
Here's call.php:
<?php
require 'connection.php';
$players2weeks = '(removed api)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $players2weeks);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
print_r($response);
echo $response->players_2weeks;
?>
I have the print_r for troubleshooting but I'm getting nothing. My apologies for a noob question by the way, I have no experience with JSON.
Appreciate the help!
I think you can use this Code :
<?php
require 'connection.php';
$players2weeks = '(removed api)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $players2weeks);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);
$responses = json_decode($response);
print_r($responses);
echo $responses['players_2weeks'];
?>
You should change the format of echo when the decoded JSON objects. and the Variable same means some clashes in the print so I changed the $response variable also...
You have to make array to json string by using the json_encode() function on the api.
For example: xxx.php (api)
$result = array(); //return array
echo json_encode($result);
Then you can get result by json array on the api caller.
var_dump(json_decode($result)); // Object
var_dump(json_decode($result, true)); // Associative array
This question already has answers here:
How to check if JSON object is empty in PHP?
(5 answers)
Closed 6 years ago.
This may sound DUPLICATE, but I tried all the answers with this same question.
I am using this API, https://smartystreets.com/docs/cloud/us-street-api
My problem is, the output of their INVALID ADDRESS is
[ ]
It is also stated in their documentation.
Now, this is my code but it's not working.
$url = 'SOME_API_HERE' ;
$url = str_replace(" ", '%20', $url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$json = json_decode($result, true);
if (!empty($json))
{
return json_decode($result, true);
}
else {
return 'invalid address';
}
Convert JSON to an array first and then check if this array is empty or not:
$data = json_decode($result, true);
return empty($data) ? 'invalid address' : $data;