Warning: INSERT command denied to user 'dbo292345962'@'74.208.16.27' for table 'watchdog' query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:12:\"user warning\";s:8:\"%message\";s:397:\"INSERT command denied to user 'dbo292345962'@'74.208.16.27' for table 'captcha_sessions'\nquery: INSERT into captcha_sessions (uid, sid, ip_address, timestamp, form_id, solution, status, attempts) VALUES (0, '71ef66f5e34b7d72f306ede98465faa6', '54.82.80.15', 1414782524, 'comment_form', '6165ee110184f4d830e637ca94795f60', 0, 0)\";s:5:\"%file\";s:62:\"/homepages/25/d199835659/htdocs/ID/modules/captcha/captcha.inc\";s:5:\"%line\";i:99;}' in /homepages/25/d199835659/htdocs/ID/includes/database.mysql.inc on line 135
cURL PHP Send POST Data in the Background | Inferno Development

cURL PHP Send POST Data in the Background

CURL, is an extension built by Daniel Stenberg that allows you to send POST/GET messages with PHP. In this tutorial we will discuss the POST method. CURL can be used to take care of problems like APIs and credit card payment processing. Things like Google API or Yahoo API can also be used this way. It's very simple and all you have to do, is have a recent version of PHP.

Now you may have thought you could send POST messages using header(). No you can't, you would usually use a form and hidden fields which would probably be the only way. Using headers to send POST messages also won't work since headers are already sent.

This makes transactions to gateways for merchant accounts and APIs instant and invisible to the user. It allows you to send XML or normal HTML or just plain text to a server for processing and then receive the information and handle it, all in the same script.

Although PHP has CURL pre-installed, your version may not have it. You should check by making a blank page on your website and naming it a php file; type the following in it:

<?php

  phpinfo();
 
?>

Scroll down until you find a CURL section for it, and see if its installed/enabled.

If you don't have it, you can get the CURL, which is compatible to almost all Operating Systems at http://curl.haxx.se/ and then compile PHP with that.

Now, let's build a little response form, as our API. This is just an example, and is not a real API. We just build this to imitate how an API will respond, and to test our CURL. We will use XML as a response to the form. It necessarily does not have to be this way, but for example, for Google API you need to do it this way. There is no CURL in this code:

<?php

  foreach($_POST as $key => $val){
   $$key = $val;
  }
  /* Here we take all the POST variables from the POST Super Global and we
  convert each POST name and its value as $$key, meaning the name of the post variable
so if its $_POST['firstname'], then this snippet will convert it to
$firstname with the value of $_POST['firstname']. Simply, it means
converting it to simple variables from the array.
  */

  if(empty($firstname)){
  header("Content-Type: text/xml");
  echo "<API>
  <error>First name was empty</error>
  </API>"
;
  exit();
  }
/* Obviously it doesn't have to be this way, It can be just a plain
text response, just remove the header and the XML tags. This script
checks if the firstname variable is empty. */

  header("Content-Type: text/xml");
  echo "<API>
  <error>Thank you for your Query</error>
  </API>"
;
  /* if everything is good we echo a thank you XML message */
 
?>

If you are not sure how to parse XML or display it, please do not use it, this is just to show you the extent of which CURL can go to. You do not need to use the XML. Just remove all headers and tags.

Now let's move on to the actual CURL script.

<?php

  foreach($_POST as $key => $val){
    $$key = $val;
  }
  $n = "\n\n";
  $inputdata = '<?xml version=\'1.0\' encoding=\'UTF-8\'?'.'>'.$n.'
  <APIcode>
    <element>
      <firstname>'
.$firstname.'</firstname>
     </element>
  </APIcode>'
;
  $x = curl_init("http://www.infernodevelopment.com/API.php");
  curl_setopt($x, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
  curl_setopt($x, CURLOPT_HEADER, 0);
  curl_setopt($x, CURLOPT_POST, 1);
  curl_setopt($x, CURLOPT_POSTFIELDS, $inputdata);
  curl_setopt($x, CURLOPT_FOLLOWLOCATION, 0);
  curl_setopt($x, CURLOPT_REFERER, "http://www.AReferrerSite.net/API35.php");
  curl_setopt($x, CURLOPT_RETURNTRANSFER, 1);
  $data = curl_exec($x);
  curl_close($x);
  echo $data;
 
?>

Ok first, we convert our POST variables to simple ones. Then we add our XML data into $inputdata.

We use curl_init() function to activate CURL, and define the API or server we are going to send to. Which is API.php in this case. We define this initialization as $x.

curl_setopt is used to indicate an option in CURL to set. There are many of these, and it would take us all day to go through most of them.

CURLOPT_HTTPHEADER, array('Content-Type: text/xml')
This means that we add an HTTP header to the CURL request. We define that our information is not POST data, but XML data. You may also add more headers, by adding extra array values.

CURLOPT_HEADER is set to 0, because we don't want to show the headers that the API sends back.

CURLOPT_POST, 1
We define that this is a POST message. We set it to 1 meaning true.

CURLOPT_POSTFIELDS, $inputdata
We add our XML data into the POST fields which are the fields that we want to send to the API.

CURLOPT_FOLLOWLOCATION, 0
We don't want to follow any links the API outputs.

CURLOPT_REFERER, "http://localhost/apiCURL.php"
This is the HTTP referer option, that we send to the API; it can be anything really, so it's possible to use this to disguise where the request comes from.

CURLOPT_RETURNTRANSFER, 1
This means that CURL will return the transcript and response without immediately printing it, but rather returning the value.

<?php

    $data = curl_exec($x);
    curl_close($x);
    echo $data;
 
?>

We then assign $data the response. And we execute the CURL request.
Then we close the connection to CURL.
Then we echo the data. Now obviously you would probably want to make an XML parser here to use $data and display the XML.

Obviously you do not have to use XML. If you are going to use a Normal POST of a form use it this way:

<?php

  $inputdata = array();
  foreach($_POST as $key => $val){
      $inputdata[] = $key."=".urlencode($val);
    }
  $query = implode("&", $inputdata);
  $x = curl_init("http://www.infernodevelopment.com/API.php");
    curl_setopt($x, CURLOPT_HEADER, 0);
    curl_setopt($x, CURLOPT_POST, 1);
    curl_setopt($x, CURLOPT_POSTFIELDS, $query);
    curl_setopt($x, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($x, CURLOPT_REFERER, "http://MyFakeSite.com/apiCURL.php");
    curl_setopt($x, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($x);
    curl_close($x);
    echo $data;
 
?>

The difference is, we have not set the header as text/xml for the Content-Type: text/xml because this is not an XML POST. It is a simple post with a simple query.

Before the implode command, we set $inputdata as an array and added the key and value of it and constructed a query string. Then with implode we combined it with &. This means that we have made a string like so:
firstname=John&lastname=Doe

Which is what we need to send the command. However, we MUST URLENCODE the value, otherwise there will be errors. So please use urlencode() function.

So basically the only difference is, the Content-Type and the urlencoded Query string.

You should use this concept to construct payment gateway scripts and API requests to things like Google API. You have now learned how to submit a POST message OR an XML POST message. You also learned the basic CURL headers and how to set it up and use it. If you have trouble, please leave a comment or keep experimenting until it works.

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.