Difference between CURL and HTTP POST

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pranavkavi
Forum Newbie
Posts: 4
Joined: Sun May 09, 2010 2:07 am

Difference between CURL and HTTP POST

Post by pranavkavi »

For a particular SMS application, I am supposed to send the following XML content to an external URL.

Code: Select all

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE MESSAGE SYSTEM "http://127.0.0.1/psms/dtd/message.dtd" >
<MESSAGE>
<USER USERNAME="mycompany" PASSWORD="mycompany"/>
<SMS UDH="0" CODING="1" TEXT="hey this is a real test" PROPERTY="" ID="1"
DLR="0" VALIDITY="0">
<ADDRESS FROM="9812345678" TO="919812345678" SEQ="1" TAG="some
client side random data" />
</SMS>
</MESSAGE>
When I send it through a simple HTTP POST(form submit), it seems to work fine. The recipient(s) recieves the message. But if I replicate the same procedure through CURL, the external url gives that the XML send does not conform to DTD. The XML content is urlencoded before being sent through CURL.

Is there a difference between a CURL POST and a normal HTTP POST that can cause the above problem?? I could really use some help....
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Difference between CURL and HTTP POST

Post by John Cartwright »

Post your code.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Difference between CURL and HTTP POST

Post by Eran »

The XML content is urlencoded before being sent through CURL
That is the default for a CURL POST(enctype="application/x-www-form-urlencoded"). You need to pass the post fields as an array for it to be sent as multipart/form-data
http://www.php.net/manual/en/function.curl-setopt.php
pranavkavi
Forum Newbie
Posts: 4
Joined: Sun May 09, 2010 2:07 am

Re: Difference between CURL and HTTP POST

Post by pranavkavi »

Code: Select all

$data = '%3C?xml%20version=%221.0%22%20encoding=%22ISO-8859-%22?%3E%3C!DOCTYPE%20 MESSAGE%20SYSTEM%20%22http://127.0.0.1/psms/dtd/messagev12.dtd.dtd%22%20%3E%3CMESSAGE%3E%3CUSER%20USERNAME=%22username%22%20PASWORD=%22XXXXX%22/%3E%3CSMS%20%20UDH=%220%22%20CODING=%221%22%20TEXT=%22The%20flight%20%23&btnG;%20<101>%20"DEL"%20to%20"BLR"%20is%20delayed%20and%20it&apos;s%20%20revised%20time%20will%20be%20informed%20later.%20Have%20a%20nice%20day!%22%20PROPERTY=%220%22%20ID=%221%22%3E%3CADDRESS%20FROM=%22919902202099%22%20TO=%22919886094024%22%20SEQ=%221%22%20TAG=%22some%20clientside%20random%20data%22%20/%3E%3C/SMS%3E%3C/MESSAGE%3E';
		
$url = "http://api.myvaluefirst.com/psms/servlet/psms.Eservice2";
$ch = curl_init();    // initialize curl handle
curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // times out after 4s
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, "data="+($data)+"&action=send"); // add POST fields
$result = curl_exec($ch); // run the whole process
curl_close($ch); 
	
echo $result;
pranavkavi
Forum Newbie
Posts: 4
Joined: Sun May 09, 2010 2:07 am

Re: Difference between CURL and HTTP POST

Post by pranavkavi »

I tried changing the CURL code to have an array, but it still showed the same error.

Code: Select all

$post_arr = array('data'=>$xml_code, 'action'=>'send');
$url = "http://api.myvaluefirst.com/psms/servlet/psms.Eservice2";
$ch = curl_init();    // initialize curl handle
curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // times out after 4s
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_arr); // add POST fields
$result = curl_exec($ch); // run the whole process
curl_close($ch); 
	
echo $result;
Post Reply