如何使用curl正确发送和接收XML? [英] How to properly send and receive XML using curl?
本文介绍了如何使用curl正确发送和接收XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是服务器端的条件:
$ b$ b
- 对服务器的请求应通过HTTP 1.1以XML格式发送。
以下要求适用于HTTP请求:
- 请求类型应为POST;
- A
Content-Length
标头应该存在,并且请求的总长度应低于16KB; - A
text / xml
;
<$ c> c $ c> Content-Type / ul>
这是我的脚本:
$ url =http: /somedomain.com;
$ xml ='<?xml version =1.0encoding =UTF-8?>
< Request PartnerID =asasdsadsaType =TrackSearch> < TrackSearch> < Title> love< / Title> < Tags> < MainGenre> Blues< / MainGenre> < / Tags> <页数=1大小=20/> < / TrackSearch> < / Request>';
$ header =POST HTTP / 1.1 \r\\\
;
$ header。=Content-type:text / xml \r\\\
;
$ header。=Content-length:.strlen($ xml)。\r\\\
;
$ header。=连接:关闭\r\\\
\r\\\
;
$ header。= $ xml;
$ 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_CUSTOMREQUEST,$ header);
$ data = curl_exec($ ch);
echo $ data;
if(curl_errno($ ch))
print curl_error($ ch);
else
curl_close($ ch);
这给我:
HTTP错误400.请求URL无效。
错误请求 - 无效网址
解决方案
帮助?
<?php
$ url =http://stackoverflow.com
$ xml ='<?xml version =1.0encoding =UTF-8?< Request PartnerID =asasdsadsaType =TrackSearch> < TrackSearch> < Title> love< / Title> < Tags> < MainGenre> Blues< / MainGenre> < / Tags> <页数=1大小=20/> < / TrackSearch> < / Request>';
$ headers = array(
Content-type:text / xml,
Content-length:。strlen($ xml),
Connection :close,
);
$ 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,$ headers);
$ data = curl_exec($ ch);
echo $ data;
if(curl_errno($ ch))
print curl_error($ ch);
else
curl_close($ ch);
?>
I've been trying to post XML and get response from the server but with no luck.
Here are the conditions on server side:
- Requests to the server should be sent as XML over HTTP 1.1.
The following requirements apply to the HTTP request:
- 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 valuetext/xml
;
Here is my script:
$url = "http://somedomain.com";
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<Request PartnerID="asasdsadsa" Type="TrackSearch"> <TrackSearch> <Title>love</Title> <Tags> <MainGenre>Blues</MainGenre> </Tags> <Page Number="1" Size="20"/> </TrackSearch> </Request>';
$header = "POST HTTP/1.1 \r\n";
$header .= "Content-type: text/xml \r\n";
$header .= "Content-length: ".strlen($xml)." \r\n";
$header .= "Connection: close \r\n\r\n";
$header .= $xml;
$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_CUSTOMREQUEST, $header);
$data = curl_exec($ch);
echo $data;
if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);
This gives me:
HTTP Error 400. The request URL is invalid.
Bad Request - Invalid URL
解决方案
Does this help?
<?php
$url = "http://stackoverflow.com";
$xml = '<?xml version="1.0" encoding="UTF-8"?><Request PartnerID="asasdsadsa" Type="TrackSearch"> <TrackSearch> <Title>love</Title> <Tags> <MainGenre>Blues</MainGenre> </Tags> <Page Number="1" Size="20"/> </TrackSearch> </Request>';
$headers = array(
"Content-type: text/xml",
"Content-length: " . strlen($xml),
"Connection: close",
);
$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, $headers);
$data = curl_exec($ch);
echo $data;
if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);
?>
这篇关于如何使用curl正确发送和接收XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文