M_cMoreland Posted March 31, 2011 Share Posted March 31, 2011 All I have this snippet of code I want to convert to CURL.. I am a bit of a newbie on this so any help is greatly appreciated. $response = do_post_request($url, $data); // This function constructs and sends an HTTP request with a provided URL and data, and returns an HTTP response object // This function uses the php_http extension function do_post_request($url, $data, $optional_headers = null) { $request = new HttpRequest($url, HttpRequest::METH_POST); $request->setBody($data); $response = $request->send(); return $response->getBody(); } // Convert the response body into an XML element so we can extract data $responseBody = new SimpleXMLElement($response); Quote Link to comment https://forums.phpfreaks.com/topic/232343-httprequestmeth_post-convert-to-curl/ Share on other sites More sharing options...
btherl Posted March 31, 2011 Share Posted March 31, 2011 This code is for passing on a post request, but you can use the relevant parts: http://davidwalsh.name/execute-http-post-php-curl If you google for "php curl post" you'll find other examples too. To get the result in a variable, set the CURLOPT_RETURNTRANSFER option too. Quote Link to comment https://forums.phpfreaks.com/topic/232343-httprequestmeth_post-convert-to-curl/#findComment-1195282 Share on other sites More sharing options...
M_cMoreland Posted April 1, 2011 Author Share Posted April 1, 2011 OK .. I know this should be really easy.. but refer to the I am a noob comment before.. here is my full page I am just a little confused on how to put Curl in place .. this is really just my lack of coding experience.. ask me about a Cisco router and I ll be great help--lol -- anyway I am trying to upload and utilize the Bing Spatial Data to post a xml file so I can attach some custom items to an entity.. <html> <head> <title>Using the Bing Spatial Data Geocode Dataflow API</title> </head> <body> <?php $key = "xxxxxxx"; $url = "http://spatial.virtualearth.net/REST/v1/Dataflows/Geocode?description=MyJob&input=xml&output=xml&key=".$key; // STEP 1 - Create a geocode job // Get the contents of an XML data file $myfile = "geocodefeed.xml"; $data = file_get_contents($myfile); // Call custom function to generate an HTTP request and get back an HTTP response $response = do_post_request($url, $data); // This function constructs and sends an HTTP request with a provided URL and data, and returns an HTTP response object // This function uses the php_http extension function do_post_request($url, $data, $optional_headers = null) { $request = new HttpRequest($url, HttpRequest::METH_POST); $request->setBody($data); $response = $request->send(); return $response->getBody(); } // Convert the response body into an XML element so we can extract data $responseBody = new SimpleXMLElement($response); // Get data (such as job id, status description, and job status) from the response $statusDescription = $responseBody->StatusDescription; $jobId = $responseBody->ResourceSets->ResourceSet->Resources->DataflowJob->Id; $jobStatus = $responseBody->ResourceSets->ResourceSet->Resources->DataflowJob->Status; echo "Job Created:<br>"; echo " Request Status: ".$statusDescription."<br>"; echo " Job ID: ".$jobId."<br>"; echo " Job Status: ".$jobStatus."<br><br>"; // STEP 2 - Get the status of geocode job(s) // Call the API to determine the status of all geocode jobs associated with a Bing Maps key echo "Checking status until complete...<br>"; while ($jobStatus != "Completed") { // Wait 5 seconds, then check the job’s status sleep(5); // Construct the URL to check the job status, including the jobId $checkUrl = "http://spatial.virtualearth.net/REST/v1/Dataflows/Geocode/".$jobId."?output=xml&key=".$key; $checkResponse = file_get_contents($checkUrl); $responseBody = new SimpleXMLElement($checkResponse); // Get and print the description and current status of the job $jobDesc = $responseBody->ResourceSets->ResourceSet->Resources->DataflowJob->Description; $jobStatus = $responseBody->ResourceSets->ResourceSet->Resources->DataflowJob->Status; echo $jobDesc." - ".$jobStatus."<br>"; } // STEP 3 - Obtain results from a successfully geocoded set of data //Iterate through the links provided with the first geocode job and extract the 'succeeded' link $Links = $responseBody->ResourceSets->ResourceSet->Resources->DataflowJob->Link; foreach ($Links as $Link) { if ($Link['name'] == "succeeded") { $successUrl = $Link; break; } } // Access the URL for the successful requests, and convert response to an XML element $successUrl .= "?output=xml&key=".$key; $successResponse = file_get_contents($successUrl); $successResponseBody = new SimpleXMLElement($successResponse); // Loop through the geocoded results and output addresses and lat/long coordinates foreach ($successResponseBody->GeocodeEntity as $entity) { echo $entity->GeocodeResponse->Address['FormattedAddress'],"<br>"; if (!$entity->GeocodeResponse->RooftopLocation) { echo $entity->GeocodeResponse->InterpolatedLocation['Longitude'].", "; echo $entity->GeocodeResponse->InterpolatedLocation['Latitude']."<br>"; } else { echo $entity->GeocodeResponse->RooftopLocation['Longitude'].", "; echo $entity->GeocodeResponse->RooftopLocation['Latitude']."<br>"; } } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/232343-httprequestmeth_post-convert-to-curl/#findComment-1195491 Share on other sites More sharing options...
M_cMoreland Posted April 1, 2011 Author Share Posted April 1, 2011 OK would it be something like this .... // Call custom function to generate an HTTP request and get back an HTTP response $response = do_post_request($url, $data); // This function constructs and sends an HTTP request with a provided URL and data, and returns an HTTP response object // This function uses the php_http extension function do_post_request($url, $data, $optional_headers = null) { $ch = curl_intit(); $request = curl_setop($ch, CURLOPT_URL, $url); $request = curl_setop($ch, CURLOPT_HEADER,0) $request = curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); $request = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $request = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $request = $data = curl_exec($ch); $response =$request->send(); return $response->getBody(); curl_close($ch) } Quote Link to comment https://forums.phpfreaks.com/topic/232343-httprequestmeth_post-convert-to-curl/#findComment-1195555 Share on other sites More sharing options...
btherl Posted April 4, 2011 Share Posted April 4, 2011 That looks good, except I think you just need to do the curl_exec(). I'm not sure that you need to send the request afterwards. Quote Link to comment https://forums.phpfreaks.com/topic/232343-httprequestmeth_post-convert-to-curl/#findComment-1196420 Share on other sites More sharing options...
M_cMoreland Posted April 4, 2011 Author Share Posted April 4, 2011 OK I am getting the following error syntax error, unexpected T_VARIABLE on line 27 which is $request = curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/232343-httprequestmeth_post-convert-to-curl/#findComment-1196599 Share on other sites More sharing options...
M_cMoreland Posted April 4, 2011 Author Share Posted April 4, 2011 I double checked Curl was working on the server.. and it appears to be working just fine.. http://go.followclix.com/bing/curltest.php <? //Three parts to the querystring: q is address, output is the format, key is the GAPI key $key = "x"; $address = urlencode("columbia MO"); //If you want an extended data set, change the output to "xml" instead of csv $url = "http://maps.google.com/maps/geo?q=".$address."&output=csv&key=".$key; //Set up a CURL request, telling it not to spit back headers, and to throw out a user agent. $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER,0); //Change this to a 1 to return headers curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($ch); curl_close($ch); echo "Data: ". $data; ?> Quote Link to comment https://forums.phpfreaks.com/topic/232343-httprequestmeth_post-convert-to-curl/#findComment-1196607 Share on other sites More sharing options...
btherl Posted April 4, 2011 Share Posted April 4, 2011 I'm assuming you fixed the T_VARIABLE error? Does your code work now? Quote Link to comment https://forums.phpfreaks.com/topic/232343-httprequestmeth_post-convert-to-curl/#findComment-1196883 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.