Page 1 of 1

Curl and Form Help

Posted: Fri Mar 02, 2007 11:05 am
by oldtimer
I am trying to use an API from a company and they tell me.

Your request needs to be a valid application/x-www-form-urlencoded message.
In short:
Set the content-type header to "application/x-www-form-urlencoded"
FormURLEncode the $datafield
POST the following for the data "cmpi_msg=$formEncodedData"

I made this code.

Code: Select all

<?
$queryString = "<REQUEST>";
$queryString = $queryString."<Version>1.6</Version>";
$queryString = $queryString."<MsgType>cmpi_msp_authenticate</MsgType>";
$queryString = $queryString."<Username>myuser</Username>";
$queryString = $queryString."<Password>abcdefg</Password>";
$queryString = $queryString."</REQUEST>";
$data=$queryString;
$this_header = array(
   "MIME-Version: 1.0",
   "Content-type: application/x-www-form-urlencoded; charset=iso-8859-1",
   "Content-transfer-encoding: application/x-www-form-urlencoded"
);
$LOGINURL = "$url";
$POSTFIELDS = "&cmpi_msg=$data"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS); 
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
$result = curl_exec ($ch);
curl_close ($ch); 
$mytest = $result;
$mytest2=htmlspecialchars($mytest);
$mytest2=nl2br($mytest2);
echo "$mytest2";
?>
but when i submit i get.

Code: Select all

<RequestMPI><ErrorNo>2010</ErrorNo>
<ErrorDesc>Invalid Request Format: Empty Request</ErrorDesc>
</RequestMPI>
Same as before.

So real question is how do you convert to the FormURLEncode


Posted: Fri Mar 02, 2007 11:17 am
by oldtimer
I also just tried.

$data=urlencode($queryString);

no go still

Posted: Fri Mar 02, 2007 1:42 pm
by feyd
$data and $queryString are never used in the code posted thus far.

Posted: Fri Mar 02, 2007 3:10 pm
by oldtimer
yes it was .


$data=$queryString;

Posted: Fri Mar 02, 2007 4:27 pm
by feyd
Have you sniffed the http headers sent if you posted the form via a normal browser?

Posted: Fri Mar 02, 2007 4:56 pm
by oldtimer
nope not sure how ot sniff headers.

I do have the sites people looking into it though. maybe they will come up with something.

i think i am the first person to try to integrate with them with php.