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.
Related
So i am trying to get values of json object from a url, when i hit that url on post man i get something like this
{
"error": "0",
"errorString": "",
"reply": {
"nonce": "5e415334832a8",
"realm": "VMS"
}
}
So, i am trying to write a php code that displays the value of nonce in the browser but it is not working
i have the following code
$getNonceUrl = "https://example.com/api/getNonce";
$getContect = file_get_contents($getNonceUrl);
$jsonNoce = json_decode($getContect, true);
$dd = $jsonNoce->reply[0]->nonce;
echo $dd;
I also did this
$ch = curl_init("https://example.com/api/getNonce");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
echo $ch->reply[0]->nonce;
But it does not seem to work still.
Below your 2 code samples with suggested corrections.
Using file_get_contents : remove 2nd argument of json_decode
$getNonceUrl = "https://example.com/api/getNonce";
$getContect = file_get_contents($getNonceUrl);
$jsonNoce = json_decode($getContect); // note removal of 2nd parameter
$dd = $jsonNoce->reply[0]->nonce;
echo $dd;
Using curl : you forgot to parse curl output with json_decode
$ch = curl_init("https://example.com/api/getNonce");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$data = json_decode($data); // added this line, because curl doesn't decode json automatically
echo $ch->reply[0]->nonce;
I have the following data I pulled from an API using PHP's file_get_contents($url), I would like to parse this data and put each comma separated value into a variable whilst looping to the next set (there are thousands of row, I simply extracted the first two), I have tried parsing methods however most have an element consisting of the datalabel:data, any assistance would be gladly appreciated.
[[1610223840000,"3.63410000","3.65100000","3.62900000","3.64150000","14194.01000000",1610223899999,"51684.84892800",195,"7619.89000000","27756.15839400","0"],[1610223900000,"3.64610000","3.65090000","3.63410000","3.65000000","2219.73000000",1610223959999,"8090.68646600",46,"1176.75000000","4290.44934900","0"]]
Ok figured this out today, for anyone who needs it
$ch = curl_init();
$url = "https://api.blahblahblah";
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result, true);
foreach ($obj as $key => $value) {
echo $value[0] . "<br>";
echo $value[1] . "<br>";
}
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'];
So I have this api: http://85.17.32.4:8707/status-json.xsl
And I want to extract a few things from it, so I started off really basic:
<?php echo json_decode('http://85.17.32.4:8707/status-json.xsl');
It gave absolutely no result. Next try:
<?php
$var = json_decode('http://85.17.32.4:8707/status-json.xsl');
var_dump($var);
It just retourned NULL.
Then I tried making a cURL function:
<?php
$url = 'http://85.17.32.4:8707/status-json.xsl';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result, true));
This also returned NULL. Do I have any further options?
Thanks.
Yes, you do have further options: check json_last_error_msg and see whether there was an issue with the json decoding:
$json = json_decode($result, true);
# check if there has been an error decoding:
if (! isset($json)) {
echo "Decoding error: " . json_last_error_msg() . PHP_EOL;
}
Output:
Decoding error: Syntax error
i.e. there is a syntax error in the JSON. You need to tell the JSON provider that they are not providing valid JSON.
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.