php Tutorials
Creating An RSS Feed With PHP And MySQL
Retrieving XML With Curl and SimpleXML
Using curl to Query Remote Servers
MySQL Basics
PHP mySQL Voting Booth
File Download Security
PHP Fundamentals

Retrieving XML With Curl and SimpleXML

Retrieving Data

Now here is the meat, building a simple curl class to handle the retrieving of the data. More information on using curl in php.

Class getData {
   public $URL;
   public $XMLRequest;
   public $XMLResponseRaw;
   public $parameters;
   public $XPath;

   function buildCurlParamString() {
       $urlstring = '';

       foreach ($this->parameters as $key => $value) {
           $urlstring .= urlencode($key).'='.urlencode($value).'&';

       if (trim($urlstring) != '') {
           $urlstring = preg_replace("/&$/", "", $urlstring);
           return ($urlstring);
       } else {
           return (-1);


In this first part we declared a few variables for use in the class. Our first function buildCurlParamString() takes the class variable parameters, which is an associated array with the key being the variable name and the value being the variable value for the request, and turns them into the query string for our POST request to the remote server. We'll set the parameters array later just know it stores the names and values of the fields to post. We loop through the arrays urlencoding the data. The urlencode function formats our data properly for transmission to the server hosting the XML feed.

Then we trim off our trailing & from the previous loop. If for any reason we aren't returning a valid urlencoded string we return -1 out of buildCurlParamString().

Introduction <<  1 2 3 4 5  >> Using curl
New Content