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.
Related
I have simple POST CURL request script,
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
The problem i am facing is, if i fetch post data from db or i fetch post data from a form, when i process data in large quantity, for example 60 times. The curl displays the output until and unless all 60 requests are completed.
I want the curl to display output one by one not all at once when the query is completed e.g.
Echo 1st Response
Echo 2nd Response
Echo 3rd Response
What you are requesting appears to be undefined.
PHP, and any other HTTP Server can only start sending data, when the data-length is know. When echoing, your server is supposed to assemble the reply, then organize a transfer.
If you want to assemble a document, that can be done, use AJAX, or any other request/reply system to assemble divs/fields each.
An example would be:
"Base document" -> [N] divs or fields. Each field requests its related procesing.
When basereceives data, it assembles in its related div.
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..
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.
I am trying to get the details of a venue in Foursquare using the venue ID, but there is something minor that isn't correct:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.foursquare.com/v2/venues/4c599c84f346c9287ff84cca?client_id=[MY_ID]&client_secret=[MY_SECRET]&v=20120609');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
$result = json_decode($contents);
var_dump($result);
curl_close ($ch);
If I change the URL to something like Google News RSS, or one of my XML feed pages, I get results, to the cURL portion is right
If I paste the above URL into my browser (with my actual ID&Secret) then I get a json formatted result with the data that I expect (correct name of venue, etc). So I know the URL is correct.
If I copy the json from the above process and put it into a variable in my code, then set $result to the decode version of that variable then I see the results properly. So I know that the decode/output bit is working.
Somewhere between retrieving the result and storing it in a variable for decoding something is going wrong. I have to assume it is something silly and simple, since all of the parts are there, but I can't figure it out.
Any help is appreciated.
The answer turned out to be that I had to add the following 2 lines:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
I would like to crab data from a website that uses an ajax request to load new data from the server into a DIV.
When I click on the button of the website, that will load new data into the website, I can see that the browser does only 1 POST request with the following post string:
AJAXREQUEST=_viewRoot&j_id376=j_id376&javax.faces.ViewState=j_id3&j_id376%3Aj_id382=j_id376%3Aj_id382&valueChanged=false&AJAX%3AEVENTS_COUNT=1&
When I do the above post request using php curl I don't get any useful data.
Does someone know how to crab data for this kind of request?
UPDATE1:
This is what I use in php:
$ch = curl_init ('http://www.website.com');
$post_string = 'AJAXREQUEST=_viewRoot&j_id376=j_id376&javax.faces.ViewState=j_id3&j_id376%3Aj_id382=j_id376%3Aj_id382&valueChanged=false&AJAX%3AEVENTS_COUNT=1&';
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
$output = curl_exec ($ch);
I don't get any results, also no errors or messages.
Your problem probably isn't with your PHP code, its more likely with what you are actually sending to the server. I'm assuming you listed website.com as a place holder for whatever service you are trying to interact with, but since you haven't listed any of the information as to where your sending the request or what your getting back I'm assuming that what your posting to the server is simply being ignored because what your sending is invalid, or incomplete, or requires further POST/GET requests. Another possibility is that your attempting to POST to a service that requires an authenticated session (the POST variables you listed could include some sort of token to identify the session) which you have not established.
I would recommend that you first test your code on a simpler "controlled test case". Setup a basic web form that returns true or something when you POST a value to it. Test your code with the simpler case first to make sure your POST code works.
Then using a debugging tool such as LiveHTTPHeaders or Firebug record the entire POST/GET request interaction with the server. It might be a good idea to first try to "replay" this interaction with a debugging tool to prove that your methodology works. Then once you know exactly what you need to do from a high level, repeat this process in your PHP code.
There is not much other advice anyone can give you with the information you have given us.