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;
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
This question already has answers here:
Curl returns true instead of value
(2 answers)
Closed 4 years ago.
I am integrating broadcasting to a messenger both right now, following the messenger platform API:
https://developers.facebook.com/docs/messenger-platform/send-messages/broadcast-messages
I am following the steps and the broadcast message gets send
The function below should return
{
"broadcast_id": <BROADCAST_ID>
}
As mentioned in the documentation, instead it looks like it just echoes it out and when I try to assign it to a variable (to record it in the database later) it turns into a boolean and returns true
public function send_message($message_creative_id, $access_token)
{
$creativeIdJSON = '{
"message_creative_id": "' . $message_creative_id . '"
}';
$api_url = 'https://graph.facebook.com/v2.11/me/broadcast_messages?access_token='.$access_token;
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $creativeIdJSON);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request
$result = curl_exec($ch);
curl_close($ch);
$response = json_decode($result);
if(isset($response))
{
return $response;
}
else
{
return false;
}
}
How can I get the broadcast ID and assign it to a variable?
From the manual for curl_exec:
Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.
So:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
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 an answer here:
Curl error: Could not resolve host: www.localhost
(1 answer)
Closed 4 years ago.
the response is coming as NULL .Here is my code:
$url = "http://www.localhost:81/dbWIP/selectApi.php?name='$name'";
$client = curl_init();
echo $client;
curl_setopt($client, CURLOPT_URL,$url);
curl_setopt($client,CURLOPT_RETURNTRANSFER,true);
curl_setopt($client, CURLOPT_HEADER, false);
$response = curl_exec($client);
curl_close($client);
$result = json_decode($response);
echo $response;
The $name var isn't properly showing try this:
$url = "http://www.localhost:81/dbWIP/selectApi.php?name=".$name;
Right now it is just showing:
$url = "http://www.localhost:81/dbWIP/selectApi.php?name='$name'";
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.