Jump to content

HttpRequest::METH_POST convert to CURL.


M_cMoreland

Recommended Posts

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);

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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)

}

Link to comment
Share on other sites

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;

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.