json response store in array using php - php

Hello I am getting json response from web using php curl. Response which I am getting is a bulk amount of data. Now I want to store some of the specific data into an array and print it in table form using php, like name, status, label etc. How can I do it?
$url = "";
$ch = curl_init();
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result = curl_exec($ch);
echo ($result);
curl_close($ch);

if you are getting a JSON response, use json_decode() to make it php readable. To make it an array (I would), use the true parameter like json_decode($result,true) and then manipulate the date from that array.
JSON-decode (php manual)

Related

PHP to download and Return Raw JSON Data?

I've been working on an tool that displays information from an API using PHP. Up until now I've been trying to echo individual fields of information one at a time in order to format them. Now I'm curious is it possible for me to somehow return just the whole unparsed JSON return, by this I mean just the raw JSON as it it. Can I somehow call that into a PHP page?
Here's an example of the JSON I'm referring to..
https://api.data.gov/sam/v4/registrations/7800853870000?return_values=full&api_key=CCHRrZMMc1s9I1dxtk4bwTPdoT4A1Xjck9w4Lii0
If you just want to output the json from https://api.data.gov/sam/v4/registrations/... on your website, you can use:
header('Content-Type: application/json');
$url = "https:///someJsonUrl";
echo file_get_contents($url);
You can use Client URL Library for this.
Here's the reference:
Client URL Library
So your code should be like this:
$ch = curl_init();
$searchURI = 'http://api.data.gov/sam/v4/registrations/7800853870000?return_values=full&api_key=CCHRrZMMc1s9I1dxtk4bwTPdoT4A1Xjck9w4Lii0';
curl_setopt($ch, CURLOPT_URL, $searchURI);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$jsonData=curl_exec($ch);
curl_close($ch);
// $jsonData is your row json data
header('Content-Type: application/json');
echo $jsonData;

Unable to perform any operation on data retrieved using curl in php

I am trying to get data from particular website. Initially I was trying to retrieve data using normal $.ajax() method but because of cross domain problem I drop that idea. Now I'm trying to get same data using PHP's curl function. I'm getting success in it, but I was expecting json response in return but I am getting some kind of json + some other string at the end of that json data. I was even trying to delete that padding with PHP's string operations but I am unable to perform any kind of operations on that data. So, is there any way of doing that? Even when I'm trying to store that $string variable in a log for any text file it is saving the html code not the respose posted below.
Here is my code:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/do/mapsearch/getResults_ajax");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'city=12&map_type=listing&latlongsearch=1&encrypted_input=UiB8IFFTIHwgUyB8IzIjICB8IG11bWJhaSB8IzMjICB8IE5SSSB8IFkgIzIxI3wgIHwgMSw0LDIsMyw5MCw1LDIyIHwgMTIgfCMxIyAgfCBPLEEsQiMzIyB8IDEsNCwyLDMsOTAsNSwyMiM1IyB8IE8sQSxCICMzMSN8ICA%3D&keyword=&sortby=&mapsearch=1&locality_array=null&building_id=&latitude=19.137212294689522&longitude=72.8479625817871&latlongsearchdistance=3&page=1');
curl_setopt($ch, CURLOPT_POST, 1);
$string = curl_exec($ch);
// it's not even printing these two lines
echo "<br /><br/><br/>";
echo "hie";
echo curl_error($ch);
curl_close($ch);
// tring to get type of response but not printing anything on web page
echo gettype($string);
?>
Here is the part of my response:
{"total":2987,"resultsCount":30,"results":[{"PROP_ID":"K19962871","SPID":19962871,"VERIFIED":"N","PROFILEID":"509882","SHORTLISTED":"N","TYPE_DISP":"Apartment","AREA_UNIT":"Sq.Ft.","AREA":580,"AREA_TYPE":"Super built-up ","LOCALITY_ID":4962,"BUILDING_ID":0,"IS_XID_ATTACHED":0,"PRICE_DISP":"86 Lac","CITY":"Mumbai Andheri-Dahisar","LOCALITY":"Jogeshwari (West)","SOCIETY_NAME":"Off S.v. Road","PROP_NAME":"Off S.V. Road","LISTING":"P","BEDROOM_NUM":1,"LATITUDE":"19.1364893","LONGITUDE":"72.8488709","PROP_INFO":{"class_label":"Dealer","dealer_url":"\/sainath-estate-mumbai-andheri-dahisar-drid-509882","text":"Sainath Estate","link":true,"vcard":false},"BLDNG_INFO":{"name":"Off S.v. Road","link":null,"label":"Society :","cls":"fwn ","lcls":null},"DEFAULT_THUMB":{"url":"http:\/\/static.99acres.com\/images\/mapsearch\/nophoto.jpg","type":"noPhoto"},"MAPPED":"N","AVAILABILITY":"Ready to Move","FURNISH":""}],"encrypted_input":"UiB8IFFTIHwgUyB8IzIjICB8IG11bWJhaSB8IzMjICB8IE5SSSB8IFkgIzIxI3wgIHwgMSw0LDIsMyw5MCw1LDIyIHwgMTIgfCMxIyAgfCBPLEEsQiMzIyB8IDEsNCwyLDMsOTAsNSwyMiM1IyB8IE8sQSxCICMxMiN8ICB8IDEgfCAxOS4xMzcyMTIyOTQ2ODk1MjIgfCA3Mi44NDc5NjI1ODE3ODcxIHwgMyAjMTQjfCAg","list_vlnk":"\/property-in-mumbai-ffid?orig_property_type=1,4,2,3,90,5,22&class=O,A,B&search_type=QS&search_location=NRI&pageid=QS&keyword_orig=mumbai&property_type=1,4,2,3,90,5,22","html2":"
Please any suggestions?
Thank you..

How to get content of a website which calls a JSON file using PHP/cURL

There is a supermarket website and I need to get list of product name and price data.
The website is: http://www.sanalmarket.com.tr/kweb/sclist/30011-tum-meyveler
However, I cannot get this content with success. Every attempt finalized with a null result. I am not familiar with cURL, but it is recommended me to overcome this issue. As I see, the product list is called with Ajax - JSON and for this reason, I should follow requests to see JSON files and their contents using PHP. ...But how?
Thank you in advance.
The code I tried:
<?php
$url="https://www.sanalmarket.com.tr/kweb/sclist/30011-tum-meyveler";
$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));
?>
Your curl request did work and you are getting html response in the $result variable. The problem is that you are treating the html response string like a valid JSON string.
instead of
var_dump(json_decode($result, true));
try
var_dump($result);
Here $result is not a valid JSON string. It is a string containing the html that the server responded. So you cannot parse it directly into an array or object without using a html parser.

PHP cURL is it possible to pass a multi-associative array?

Is it possible to send a multi-associative array to a page using cURL in php?
I am able to pass an array, but the following happens:
// Open Connection
$ch = curl_init();
// Set the URL
curl_setopt($ch, CURLOPT_URL, $this->config['submission']['eyerys']);
// Set the number of fields being sent:
curl_setopt($ch,CURLOPT_POST,count($this->call['info']));
// The string to send:
curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
// Return transfer:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// SSL verification:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// Execute the post:
$result = curl_exec($ch);
$this->pre($result);
// Close connection:
$curl_close($ch);
I get the following output:
Array
(
[info] => Array
[answers] => Array
[errors] => Array
)
Nope, since curl cannot know how you want to encode it. Not every server-side language/framework uses the same way. I think PHP is the only language where the user can create an array by simply sending data with keys containing []. For example. in the python world one would just send the same value twice and then use a different function (such as .getlist('key') - depends on the framework though) to access the array instead of just a single value.
If you have control over the remote script, consider using something standardized such as JSON. Instead of sending a formencoded POST string either send a pure JSON body or a single formencoded POST value containing the JSON.
If you don't, you'll most likely have to encode the POST data on your own.

cURL returning XML or JSON

I've got a form say here - http://example.com/palreg.php
Once people register I'll be sending them an email with the link that will allow them to edit their details (I know it's a crazy way of doing things, but I am working on someone else's code so don't mind) for instance the url as such http://example.com/palreg.php?paliD=1234, and when they go to that page the form will be populated with their information so that they can make changes.
Now the problem is that the DB is in a different site and the information has to be passed to that site to perform a select action, to do so I use cURL to send the information like so
$url = "http://example2.com/processXML.php";
$xmlStr will be like this
<table>tab_name</table>
<action>select</action>
<palid>1234</palid>
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'xmlstr='.$xmlStr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
On the other end (http://example2.com/processXML.php) I convert the xml to an array and perform the query to select pal information based on the id sent.
The real question now is how do I send the retrieved pal information (as xml or json or array) back so that i can populate the form with the returned data.
Also can I do a return $dataArray; in processXML.php and be able to grab it?
OK to make things a bit clearer, what I do in processXML.php is
retrieve the result set and do this
print json_encode($resultArray);
not should I print or return
Thank you, and do let me know if things aren't clear.
just encode it as you want, and echo it to the page. your $data= will contain the echoed contents. Personal preference has been to use JSON since it's so easy to throw around. as in
//Bunch of DB stuff..
$row=mysql_fetch_however_you_handle_it();
echo json_encode($row);
//on your receiving end, that just did the cURL send,
$row=json_decode($data,true);//decode the JSON straight into an array...
When you pull the information from the database and process it in processXML.php, add the results to an array, use json_encode() to convert the array to JSON, and then echo the results to the page.
Just echo out the data you need to send back to the form, in your format of choice, and fetch that response in thepalreg.php.

Categories