Re: XML API, libcurl, POST Help Needed

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Richard Luckhurst wrote:
> Hi List
> 
> I have been given a rather urgent problem to solve for one of our clients. I
> have to be able to connect to a server that deals with a section of the travel
> industry and send and receive XML data. The API is quite poorly written and I am
> finding it hard to get sense out of the companies help desk. I would appreciate
> the help of the list in getting me pointed in the right direction.
> 
> The API expects a custom header and then a request. I have built the header and
> I think it is OK. The request is an XML request and this is where I believe I am
> having problems. I am getting an error response from the server telling me I
> have invalid XML.
> 
> I am really stumped with the XML part. I have no idea how to code this section
> and it appears what I am doing is not correct. I have spent days searching the
> net looking for clues and so far have not succeeded.
> 
> I would appreciate help and guidance. If anyone needs any more information
> please do not hesitate to ask.
> 
> This is what I have so far (note I have hidden usernames and passwords)
> 
> <?php
> 
> $url = "stagingxml.infosys.de";
> $port = "10816";
> 
> /*
> * HTTP Header Components
> * Each part of the custom header is defined in the API document
> */
> 
> $length = "Content-Length: 502";

The way I see it your Content-Length is wrong - the
length of your request in this instance would 380 bytes,
based on the length of $xmlstr. but that's probably not right
either.

I imagine you need to set Content-Length dynamically,
assuming curl doesn't do that for you ...

as a side note: why bother setting all these vars then stricking
them in an array - why not just stick the strings directly in the array?
additionally if you don't need to use double quotes (for variable
interpolation) single quotes are [marginally] faster [in theory] because
no variable interpolation is attempted on them.

e.g.

$headers = array(
	'Accept-Encoding: gzip',
	'Transfer-Encoding: identity',
	'Accessmode: agency',
	'Accessid: XXXXXXX XXXXXXX_layout',
	'Accept: application/xml',
	// etc
);

> $a_encoding = "Accept-Encoding: gzip";

not related to your current issue, but are your really accepting
a gzip'ed reply? this might cause you headaches once you have the
current problem solved.

> $t_encoding = "Transfer-Encoding: identity";
> $accessmode = "Accessmode: agency";
> $accessid = "Accessid: XXXXXXX XXXXXXX_layout";
> $accept = "Accept: application/xml";
> $charset = "Accept-Charset: iso-8859-1, utf-8";
> $host = $test_url;
> $session = "Session: ";
> $api = "Api-Version: 3.4";
> $connection = "Connection: Close";
> $authorization = "Authorization: A Base 64 representation of username, password";
> $authmode = "Authmode: pwd";
> 
> /*
> * Build HTTP Header to use in CURLOPT_HTTPHEADER
> */
> 
> $headers[] = $length;
> $headers[] = $a_encoding;
> $headers[] = $t_encoding;
> $headers[] = $accessmode;
> $headers[] = $accessid;
> $headers[] = $accept;
> $headers[] = $charset;
> $headers[] = $session;
> $headers[] = $api;
> $headers[] = $connection;
> $headers[] = $authorization;
> $headers[] = $authmode;
> 
> $xmlstr = '<?xml version="1.0" encoding="UTF-8"?><fareRequest da="false"><vcrs/><fareTypes/><tourOps/><flights><flight depApt="FRA" dstApt="LON" depDate="2007-01-26"/></flights><paxes><pax surname="Smith" firstname="John" dob="1945-12-12" gender="M"/></paxes><paxTypes/><options><limit>20</limit><offset>0</offset><waitOnList><waitOn>ALL</waitOn></waitOnList></options><coses/></fareRequest>';
> 
> $curl = curl_init();
>         curl_setopt($curl, CURLOPT_URL, $url);
>         curl_setopt($curl, CURLOPT_PORT, $port);
>         curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
>         curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
>         curl_setopt($curl, CURLOPT_POST, 1);
>         curl_setopt($curl, CURLOPT_POSTFIELDS, urlencode($xmlstr));

I am guessing that the urlencoded $xmlstr should be given a POST parameter name,
what that name is I don't know but the previous line should look something like
this (*I think*):

curl_setopt($curl, CURLOPT_POSTFIELDS, array('xml' => urlencode($xmlstr)));

you might consider point the curl request to a script you control,
so that you can see exactly what the request is sending.

	e.g. $url = 'http://yourdomain.tld/your-curl-request-test.php';

... and have your-curl-request-test.php write all it's recieved headers, etc
to a log file.


>         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
>         
>         curl_setopt($curl, CURLOPT_TIMEOUT, 40);
>         curl_setopt($curl, CURLOPT_VERBOSE, 1);
> 
>         $response = curl_exec($curl);
>                 if(curl_errno($curl))
>                 {
>                         print curl_error($curl);
>                 }
>                 else
>                 {
>                 curl_close($curl);
>                 }
>         
>         print "$response\n\n\n";
> 
> ?>
> 
> I thank you in advance for any help.
> 
> 
> 
> Regards,
> Richard Luckhurst
> Exodus Systems - Sydney, Australia.
> rluckhurst@xxxxxxxxxxxxx
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux