Return to Snippet

Revision: 52926
at November 6, 2011 06:16 by focal55


Updated Code
<?php

/******************************************
 * POST TO REMOTE SCRIPT
 ******************************************/

//fake data to send via cURL
$post = 'var1=hello&var2=world';
//replace with your post destination url
$url = "http://example.com/script.php";
//start cURL
$ch = curl_init();
//define curl: set where the post is going
curl_setopt($ch, CURLOPT_URL, $url);
//define curl: set that you want the response
//to be store in a variable and not display on screen
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//define curl: set what you are sending
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

//If the post was not successful curl_exec will return false.
//If successful store response in variable 
if ($xmlResponse = curl_exec($ch)) {
  //close the connection  
  curl_close($ch);
  
  //Trim the response from excess white space
  //NOTE:It may be necessary to do further sanitizing of the string
  $xmlResponse = trim($xmlResponse);
  //enable error reporting
  libxml_use_internal_errors(true);
  //parse the xml response string
  $xmldata = simplexml_load_string($xmlResponse);
  //store the original xml in an array
  $xml = explode("\n", $xmlResponse);
  //if simplexml_load_string() fails, it returns false.
  //if it returns false, collect the errors and print them to the screen
  if (!$doc) {
    $errors = libxml_get_errors();
    foreach ($errors as $error) {
      echo display_xml_error($error, $xml);
    }
    libxml_clear_errors();
  }else{
    //Successfull parsed the xml string
    print_r($doc);
  }
}else{
  //There was a problem with cURL executing. Do you have cURL on this server?
  echo "cURL was not able to execute";
  //close connection
  curl_close($ch);
}

//Helper Function For Displaying the Errors
function display_xml_error($error, $xml){
  $return  = $xml[$error->line - 1] . "\n";
  $return .= str_repeat('-', $error->column) . "^\n";
  switch ($error->level) {
    case LIBXML_ERR_WARNING:
    $return .= "Warning $error->code: ";
    break;
    
    case LIBXML_ERR_ERROR:
    $return .= "Error $error->code: ";
    break;
    
    case LIBXML_ERR_FATAL:
    $return .= "Fatal Error $error->code: ";
    break;
  }
  $return .= trim($error->message) .
  "\n  Line: $error->line" .
  "\n  Column: $error->column";
  if ($error->file) {
    $return .= "\n  File: $error->file";
  }
  return "$return\n\n--------------------------------------------\n\n";
}
?>

Revision: 52925
at November 6, 2011 06:15 by focal55


Initial Code
<?php

/******************************************
 * POST TO REMOTE SCRIPT
 ******************************************/

//fake data to send via cURL
$post = 'var1=hello&var2=world';
//replace with your post destination url
$url = "http://example.com/script.php";
//start cURL
$ch = curl_init();
//define curl: set where the post is going
curl_setopt($ch, CURLOPT_URL, $url);
//define curl: set that you want the response
//to be store in a variable and not display on screen
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//define curl: set what you are sending
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

//If the post was not successful curl_exec will return false.
//If successful store response in variable 
if ($xmlResponse = curl_exec($ch)) {
  //close the connection  
  curl_close($ch);
  
  //Trim the response from excess white space
  //NOTE:It may be necessary to do further sanitizing of the string
  $xmlResponse = trim($xmlResponse);
  //enable error reporting
  libxml_use_internal_errors(true);
  //parse the xml response string
  $xmldata = simplexml_load_string($xmlResponse);
  //store the original xml in an array
  $xml = explode("\n", $xmlResponse);
  //if simplexml_load_string() fails, it returns false.
  //if it returns false, collect the errors and print them to the screen
  if (!$doc) {
    $errors = libxml_get_errors();
    foreach ($errors as $error) {
      echo display_xml_error($error, $xml);
    }
    libxml_clear_errors();
  }else{
    //Successfull parsed the xml string
    print_r($doc);
  }
}else{
  //There was a problem with cURL executing. Do you have cURL on this server?
  echo "cURL was not able to execute";
  //close connection
  curl_close($ch);
}

/**
 * Helper Function For Displaying the Errors
**/
function display_xml_error($error, $xml){
  $return  = $xml[$error->line - 1] . "\n";
  $return .= str_repeat('-', $error->column) . "^\n";
  switch ($error->level) {
    case LIBXML_ERR_WARNING:
    $return .= "Warning $error->code: ";
    break;
    
    case LIBXML_ERR_ERROR:
    $return .= "Error $error->code: ";
    break;
    
    case LIBXML_ERR_FATAL:
    $return .= "Fatal Error $error->code: ";
    break;
  }
  $return .= trim($error->message) .
  "\n  Line: $error->line" .
  "\n  Column: $error->column";
  if ($error->file) {
    $return .= "\n  File: $error->file";
  }
  return "$return\n\n--------------------------------------------\n\n";
}
?>

Initial URL

                                

Initial Description

                                

Initial Title
PHP - cURL receives xml

Initial Tags
curl, xml

Initial Language
PHP