James
James

Reputation: 1

Post form data to external url + curl

I have this form that I am trying to use to post data to an external url. I do have some very basic knowlegde of using php curl. So far if I use this code that I have written:

<?php
    if ($_POST['request_callback'])
    {
      $customer_name = cleaninput($_REQUEST['customer_name'],"text");
      $debtor_id = cleaninput($_REQUEST['debtor_id'],"number");
      $telephone_number = cleaninput($_REQUEST['customer_number'],"number");

      if ($_POST['callme_now'] == '1') {
         $callback_time = date('y-m-d ' . $_POST['hour_select'] . ':' . $_POST['minute_select'] . ':s');
      } else {
         $callback_time = date('y-m-d H:i:s');
      }

      // Send using CURL 
      $ch = curl_init(); 
      curl_setopt( $ch, CURLOPT_URL, "http://www.myjoomla.eo/test.php"); // URL to post 
      curl_setopt ($ch, CURLOPT_POST, 1);
      curl_setopt ($ch, CURLOPT_POSTFIELDS,         "Name=$customer_name&Debtor_ID=$debtor_id&Telephone_Number=$telephone_number&CallBack_Time=$callback_time");
      curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
      $result = curl_exec( $ch ); // runs the post 
      curl_close($ch);
      echo "Reply Response: " . $result; // echo reply response
     }
?>

So far, it does post to the file and the results are display. Now how do I format the data that has been posted into xml format? Ideally, I am trying to acheive something like this an xml that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<CallRequest>
<ProjectName>Test</ProjectName>
<ContactNumberToDial>07843088348</ContactNumberToDial>
<DateTimeToDial></DateTimeToDial>
<ListSource>WebLead</ListSource>
<AgentName></AgentName>
<AddToList>False</AddToList>
<SpecificAgent>False</SpecificAgent>
<DBField>
    <FieldName>Name</FieldName>
    <FieldValue>Testing</FieldValue>
</DBField>
</CallRequest>

Anyone have an Idea of what to do here?

Thanks,

James

Upvotes: 0

Views: 2322

Answers (3)

James
James

Reputation: 1

Hi Sorry for taking a while in getting back. Been trying to figure this out in a few way. What I have been told is that the client wants to post an xml string to the given URL. When looking at sample pages, they have got 3 examples of what could be then. There is an example with SOAP 1.1 which display the request and response, an example with SOAP 1.2 request and response, an HTTP GET request and response sample and an HTTP POST request and response sample.

I have opted for the latter which I feel will be the easiest to work with and I am using PHP curl.

The HTTP POST example is this:

Request:

POST /ClickToCall/CallRequest.asmx/Call HTTP/1.1
Host: 194.217.1.2
Content-Type: application/x-www-form-urlencoded
Content-Length: length

xmlString=string

Response:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">string</string>

When I enter the xmlString manual on the url's test page, I get the necessary responses.

The xmlString looks like this:

<?xml version="1.0" encoding="utf-8"?><CallRequest><ProjectName>Noble Test</ProjectName><ContactNumberToDial>07843088348</ContactNumberToDial><DateTimeToDial>2009-12-10 18:30:53</DateTimeToDial><ListSource>WebLead</ListSource><AgentName></AgentName><AddToList>False</AddToList><SpecificAgent>False</SpecificAgent><DBField><FieldName>Name</FieldName><FieldValue>NobleTesting</FieldValue></DBField></CallRequest>

When I use my code however, I get no response at all.

This is the code I am using:

<?php

if ($_POST['request_callback'])
{
$customer_name = cleaninput($_REQUEST['customer_name'],"text");
$debtor_id = cleaninput($_REQUEST['debtor_id'],"number");
$telephone_number = cleaninput($_REQUEST['customer_number'],"number");

if ($_POST['callme_now'] == '1') {
    $callback_time = date('y-m-d ' . $_POST['hour_select'] . ':' .    $_POST['minute_select'] . ':s');
} else {
    $callback_time = date('y-m-d H:i:s');
}

// XML data as string 
$request = '<?xml version="1.0" encoding="utf-8"?>'; 
$request .= '<CallRequest>'; 
$request .= '<ProjectName>Nobel Test</ProjectName>'; 
$request .= '<ContactNumberToDial>' . $telephone_number . '</ContactNumberToDial>'; 
if (isset($_POST['callme_now'])) {
$request .= '<DateTimeToDial></DateTimeToDial>';
} else {
$request .= '<DateTimeToDial>' . date('Y-m-d ' . $_POST['hour_select'] . ':' .     $_POST['minute_select'] . ':s') . '</DateTimeToDial>';
}
$request .= '<ListSource>WebLead</ListSource>'; 
$request .= '<AgentName></AgentName>'; 
$request .= '<AddToList>False</AddToList>'; 
$request .= '<SpecificAgent>False</SpecificAgent>'; 
$request .= '<DBField>';
$request .= '<FieldName>Customer Name</FieldName>';
$request .= '<FieldValue>' . $customer_name . '</FieldValue>';
$request .= '</DBField>';
$request .= '</CallRequest>';

// Create Headers 
$header[] = "Host: www.myjoomla.eo"; 
$header[] = "Content-type: application/x-www-form-urlencoded"; 
$header[] = "Content-length: ". strlen($request) . "\r\n"; 

$header[] = $request;

$loginUsername = "username";
$loginPassword = "password"; 

// Send using CURL 
$ch = curl_init(); 
curl_setopt( $ch, CURLOPT_URL, "http://194.217.1.2/ClickToCall/CallRequest.asmx/Call"); // URL to post 

curl_setopt( $ch, CURLOPT_USERPWD, "$loginUsername:$loginPassword"); //login curl_setopt( $ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); // return into a variable curl_setopt( $ch, CURLOPT_POST, 1); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt ($ch, CURLOPT_POSTFIELDS, $header);

$result = curl_exec( $ch ); // runs the post curl_close($ch);

echo "Reply Response: " . $result; // echo reply response echo "

 "; echo "

";
   print_r($header);
   echo "
";

// return $result; }

Does anyone see anything with with the above code? Thanks

Upvotes: 0

Bill Huertas
Bill Huertas

Reputation: 746

I agree with jkndrkn - it seems cURL is correct, it's a matter of the output from test.php. IBM has a great article about reading/writing/parsing XML with PHP, check it out here.

Upvotes: 0

jkndrkn
jkndrkn

Reputation: 4062

An XML library I have used in the past that allows you to create XML using PHP is XmlWriter. This library was originally written to work with PHP4. You'll find that its name conflicts with that of a built-in PHP5 class so you'll need to change both the class declaration and the constructor to something else.

Hope that helps!

Upvotes: 1

Related Questions