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
Related
This question already has answers here:
Get JSON object from URL
(11 answers)
Closed 2 years ago.
I am creating a Courier tracking plugin and get data using their API. The output they are returned is in JSON format. Here is the courier API
$curl_handle = curl_init();
// For Direct Link Access use below commented link
//curl_setopt($curl_handle, CURLOPT_URL, 'http://new.leopardscod.com/webservice/trackBookedPacket/?api_key=XXXX&api_password=XXXX&track_numbers=XXXXXXXX'); // For Get Mother/Direct Link
curl_setopt($curl_handle, CURLOPT_URL, 'http://new.leopardscod.com/webservice/trackBookedPacket/format/json/'); // Write here Test or Production Link
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, array(
'api_key' => 'your_api_key'
'api_password' => 'your_api_password'
'track_numbers' => 'string' // E.g. 'XXYYYYYYYY' OR 'XXYYYYYYYY,XXYYYYYYYY,XXYYYYYY' 10 Digits each number
));
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
How to echo the values get from this curl command?
Regards
Please use json_decode function after $buffer = curl_exec($curl_handle);
$buffer = curl_exec($curl_handle);
$json = json_decode($buffer, true);
print_r($json);
You can get PHP object array
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I am trying to get an api(steam api) response as a variable.
{"response":{"players":[{"steamid":"XXXXXXXXXXXXXXXXXXXX","communityvisibilitystate":3,"profilestate":1,"personaname":"The value I want to get","lastlogoff":XXXXXXXXXX,"profileurl":"XXX","avatar":"X","avatarmedium":"X","avatarfull":"X","personastate":1,"realname":"X","primaryclanid":"X","timecreated":XXXXXXXXXX,"personastateflags":0}]}}
Formatted: https://pastebin.com/8QtLzwVX
Currently I am using the following code to get the array:
$url="http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&steamids=" . $sid;
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
I don't know how to get the personaname value from the JSON I get in $result and put it in a variable like $personaname.
try with this one:
<?php
$json_array = json_decode($result, true);
echo $json_array['response']['players'][0]['personaname'];
This question already has answers here:
How to decode a json response from API
(3 answers)
Closed 4 years ago.
I want to know how can I get any value from the JSON array I am getting from an API.
I am calling data like this:
//initializing curl object
$curl = curl_init();
//adding fields to the curl object to enter the site
curl_setopt($curl, CURLOPT_URL, $my_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
//executing the curl call and getting data back
$json = curl_exec($curl);
curl_close($curl); // close the connection
I am getting this json array printed on my html:
[{"id":1,"name":"Books","description":null,"reference":null,"status":"active","category":{"id":"5048","name":"Ventas"},"price":[{"idPriceList":"1","name":"General","type":"amount","price":"200.0000"}],"tax":[]},{"id":2,"name":"pencil","description":null,"reference":null,"status":"active","category":{"id":"5048","name":"Ventas"},"price":[{"idPriceList":"1","name":"General","type":"amount","price":"5000.0000"}],"tax":[]}]
what I want is to get only one or two values from that json array, I mean I tried this:
$code = json_decode($json, true);
echo $code[0]['id'];
but it seems like it doesn´t work because I am still getting the same JSON array printed in my HTML and not the value I want to be displayed. I need to hide all the other values and just let displayed the one I want.
You need to tell curl to return the value, not print it:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
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;
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.