I'm trying to use php to get an external json feed to use within my site, I've tried using get_file_contents and also CURL. But both are returning the same thing.
�~�X�ݘ]O�0��'��\�fԳ�o�JK7>�h�!M ��l�S�d�B���iKHC�Pi�M���W��G>���I�YD��G��F���,F��O��3a7�E�C��GC���MB������Q�&����O���ك+��tzf�c�/ ���<-���"1�!(۩�[F��w�m��a�8t}*�y��fBr�O;�{cۉ�s�B��~���l�� ��v��#^&��#���,ӊ#ԙ��/MO����$Lj��%,]�9Js��z�[I�<8w6�%���Ջ�b�y�;ӄ�f"UM�!�ipC�) �|���~����}p���w�R�7D�eޑ��Oΐ��LS�5��.�{[j�� 3|D�h�FB'b��=���G~H.�}D����� �4�ՇpGA;� VMl��<�|�,6�R,N}J�ߘ��d�x�a��.�O܊���Z���j�x����W燭���n��+<Ό�* �4 _N|�}�5�a��Aմ�����p��1V]��j����hxA<7,I. &Ϭ2�~y�LD�+6��z�QLQ�9T��A��V�5���L&�,�JNJfi��q5J��� +P��:�<�:���Q⋊ �[*0�Z(Q E��K�u��Ҍ�[�d�W��� �2&�˒���+�R�|�\"n6]_ �����z���(���������b3%,�T��r��U#��Z-�[��]լ(�k6S֚N��-;�{E��?�VET4`���V ��1�c���%�XJ�[L�0����ė��`$S�n�d\�\�k}�Z�G��B7 �^4J��k���?���W;
If I add json_decode, it echoes out nothing at all.
Here's the code I'm using:
$apistr = 'https://remitradar.com/JsonRequests.aspx?action=getOnlineQuotes&companyKey=23e9b66aspp6z&countryFrom=AU&countryTo=FJ¤cyFrom=AUD¤cyTo=FJD&amount=200';
#get file contents
$json = file_get_contents($apistr);
echo '<pre>';print_r($json);echo '</pre>';
#curl 1
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$apistr);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$jsonData = json_decode(curl_exec($ch));
curl_close($ch);
echo '<pre>CURL 1';print_r($jsonData);echo '</pre>';
#curl 2
$ch = curl_init($apistr);
curl_setopt($ch,CURLOPT_MUTE,1);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type:text/xml'));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$output = json_decode(curl_exec($ch));
curl_close($ch);
echo '<pre>CURL 2';print_r($output);echo '</pre>';
I'm not sure if the problem is on my end, or their end. But going to the URL directly works fine.
Their server uses gzip compression
You must handle this
#curl 1
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$apistr);
//curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_ENCODING , "gzip"); // handle gzip
$data = curl_exec($ch);
$data = json_decode($data);
curl_close($ch);
echo '<pre>CURL 1';print_r($data);echo '</pre>';
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 am trying to senddata to a url by using curl with codeigniter. I have successfully implemented the code for sending the data as below.
function postToURL($reg_no, $data)
{
$url = 'http://localhost/abcSystem/Web_data/viewPage';
$send_array = array(
'reg_no' =>$reg_no,
'data' =>$data,
);
$fields_string = http_build_query($send_array);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 600);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$post_data = 'json='.urlencode(json_encode($send_array));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
if (curl_errno($ch)) {
die('Couldn\'t send request: ' . curl_error($ch));
} else {
$resultStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($resultStatus == 200) {
print_r('success'); // this is outputting
return $output;
} else {
die('Request failed: HTTP status code: ' . $resultStatus);
}
}
curl_close($ch);
}
The out put is success. I want to see if this post data can be retrieved. So I tried to change the above url controller file as below.
$fp = fopen('php://input', 'r');
$rawData = stream_get_contents($fp);
echo "<pre>";
print_r($rawData);
echo "</pre>";
But nothing is printing. I want to get the data of posting. Please help me on this.
Your Written code
$fp = fopen('php://input', 'r');
$rawData = stream_get_contents($fp);
echo "<pre>";
print_r($rawData);
echo "</pre>";
is not to Print or capture Posted Data . Because you are dealing with Current Page PHP INPUT Streaming , whereas you are posting data on Other URL . So what you need to do is just Put a log of posted data in File. Use below code after $output = curl_exec($ch)
file_put_contents("posted_data.txt", $post_data );
This way you will be able to write your each post in file Posted_data.txt File - Make sure you give proper File Permission. If you want to keep trace of each POST than just make the file name dynamic so per API Call it can write a log.
Another option is to save the $post_data in DATABASE - Which is not suggestable from Security point of view.
Where ever you are calling your postToURL() function, you need to output the result of it.
For example:
$output = postToURL('Example', array());
echo $output;
This is because you are returning the curl output rather outputting it.
#input url
$url = 'http://www.example.com';
#get the data
$json = file_get_contents($url);
$contents = utf8_encode($json);
#convert to php array
$php_array = json_decode($json);
var_dump($php_array);
exit;
I'm trying to decode a website but once I decode it my page comes up as NULL, does anyone know how I can fix it? Thanks
In your case http://www.example.com this URL returns 404 error. so file_get_contents($url) get null value.
$url = 'http://www.example.com';
$json = file_get_contents($url); // HTTP 404
echo $json; //returns null
This works fine
<?php
$url = 'http://www.example.com';
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $url);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$php_array = json_decode($output, true); // true for returning to an array
echo "<pre>";
print_r($php_array);
echo "</pre>";
Extra Tip:
I too faced null issue sometimes. You could ask json_last_error() to get definite information.
You can use CURL request here:
Example:
<?php
$url = 'http://www.example.com'; // your url
$ch = curl_init(); // initiate
curl_setopt($ch, CURLOPT_URL,$url); // curl url
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch); // curl output
$php_array = json_decode($result,true);
var_dump($php_array);
?>
Output:
array(3) { ["timestamp"]=> string(8) "10:38:38" ["error_num"]=> int(404) ["error_msg"]=> string(20) "File Not Found Error" }
CURL give you more options to fetch remote content and error checking than to file_get_content.
I wanted to try to get data from a JSON string which is loaded from another page. I currently have used Curl to get the data from the webpage but I can't acces the data in it.
I've already tried:
var_dump(json_decode($result->version, true));
var_dump(json_decode($result[3][0]["date"], true));
But this does't seem to work as it always returns NULL
$url="https://roosters.deltion.nl/api/roster?group=AO2B&start=20160125&end=20160201";
// 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);
// Will dump a beauty json :3
var_dump(json_decode($result, true));
First decode the JSON, then get the properties you want. Like this:
$yourObject = json_decode($result);
var_dump($youObject->version);
this is working for me.
<?php
$url = "https://roosters.deltion.nl/api/roster?group=AO2B&start=20160125&end=20160201";
// 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);
// Will dump a beauty json :3
$data = json_decode($result);
//echo $data->data[0]['date'];
echo "<pre>";
print_r($data->data[0]->date);
}
?>
if you want to get date of all index then try this in loop.
Firstly if your using GET there is no need to use CURL,
$result = file_get_contents(https://roosters.deltion.nl/api/roster?group=AO2B&start=20160125&end=20160201);
Will work just as well without any of the overhead. I suspect that your CURL isn't returning the page content so using file_get_contents() will fix it.
I have this really long JSON string: http://pastebin.com/2jJKSGHs , which is being pulled from a music API.
I have this code set up to parse it ( http://pastebin.com/EuJtuhHg ):
$url = "https://api.discogs.com/database/search?type=artist&q=pink[keyandsecretredacted]";
//initialize the session
$ch = curl_init();
//Set the User-Agent Identifier
curl_setopt($ch, CURLOPT_USERAGENT, 'YourSite/0.1 +http://your-site-here.com');
//Set the URL of the page or file to download.
curl_setopt($ch, CURLOPT_URL, $url);
//Ask cURL to return the contents in a variable instead of simply echoing them
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Execute the curl session
$output = curl_exec($ch);
//close the session
curl_close ($ch);
//decode and print output
$output = json_decode($output2, true);
echo($output['results'][0]['title']);
When I insert the contents of the JSON string directly into my code, the json_decode works perfectly on it. But when I try to grab it from the API using the method above, nothing prints on my page -- it's just empty. Printing out json_last_error returns "0", so it's not detecting any errors.
Any ideas why this might be happening?
Replace
$output = curl_exec($ch);
with
$output2 = curl_exec($ch);
Otherwise $output2 isn't defined, and json_decode is using an undefined variable:
$output = json_decode($output2, true);