29
Jul
2011

Posting form data with Curl

So this is something I struggled with for whatever reason. Taking form data that  was posted and re-posting to another server. I am just going to post up the code first and then kind of step through it.

$ch = curl_init();

// Lets setup the data we need to pass
$postValues   = array('postedData' => $someArrayOfData);

// define the URL for upload file
curl_setopt($ch, CURLOPT_URL, 'http://yourdomain.com/api/somefile.php');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postValues);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");
$response = curl_exec($ch);
curl_close($ch);

header ("Content-Type:text/xml");
echo($response);

So basically what is happening is at first we build an array of all the values we want to post. Then we pass that variable into the config for Curl. At that point we set, the URL, and the “RETURNTRANSFER” setting, letting Curl know we want any response back from the server that we posted to. Once that is done, we can just set the header and echo out the results, or we could pass that info back into a variable to be used elsewhere.

While this is pretty brief, it was more so just to get a couple thoughts down for myself. if you have any questions though, please feel free to leave a comment or jsut email me.

This entry was posted in Programming and tagged , , by Chris. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

flickr

  • IMG_3625
  • IMG_3582
  • IMG_3573
  • IMG_3558
  • IMG_2536
  • IMG_1890
  • IMG_2141

twitter