Jump to content
php.lv forumi

Kā parerizi nosūtīt un saņemt atpakaļ XML ar curl?


fjckls

Recommended Posts

Man jānosūta xml uz serveri un tad attiecīgi serveris atgriež man xml, bet kautkas man te nestrādā, jo visu laiku met ārā "Bad Request - Invalid URL"

Te būs mans kods:

 

$url = "http://www.domeens.com";
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<Request>
   	<Tags>blah blah</Tags>
</Request>';

$header  = "POST HTTP/1.1 \r\n";
$header .= "Content-type: text/xml \r\n";
$header .= "Content-length: ".strlen($xml)." \r\n";

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
$data = curl_exec($ch);
if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);
echo $data;

 

 

Lai viss parezi darbotos, šāds ir servera nosacījumi:

- Requests to the server should be sent as XML over HTTP 1.1

- The request type should be POST;

- A Content-Length header should be present, and the total length of the request should be below 16KB;

- A Content-Type header should be present, containing the media type value text/xml;

Edited by fjckls
Link to comment
Share on other sites

Nu beidzot sanāca, hvz vai šitais ir tas pareizākais veids, bet nu strādā vismaz :):

 

$url = "http://www.domeens.com";
$xml = '<?xml version="1.0" encoding="UTF-8"?><Request><Tags>blah blah</Tags></Request>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch,CURLOPT_HTTPHEADER, array('Content-type:text/xml'));
$data = curl_exec($ch);
if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...