php 如何使用 curl 正确发送和接收 XML?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7916184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to properly send and receive XML using curl?
提问by bukowski
I've been trying to post XML and get response from the server but with no luck.
我一直在尝试发布 XML 并从服务器获得响应,但没有运气。
Here are the conditions on server side:
以下是服务器端的条件:
- Requests to the server should be sent as XML over HTTP 1.1.
- 对服务器的请求应作为 XML 通过 HTTP 1.1 发送。
The following requirements apply to the HTTP request:
以下要求适用于 HTTP 请求:
- 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
;
- 请求类型应该是POST;
Content-Length
应该有一个header,请求的总长度应该在16KB以下;- 甲
Content-Type
头应该存在,包含媒体类型值text/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
回答by James
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);
?>
回答by Michael Mior
From the documentation.
从文档中。
CURLOPT_CUSTOMREQUEST
A custom request method to use instead of "GET" or "HEAD" when doing a HTTP request. This is useful for doing "DELETE" or other, more obscure HTTP requests. Valid values are things like "GET", "POST", "CONNECT" and so on; i.e. Do not enter a whole HTTP request line here. For instance, entering "GET /index.html HTTP/1.0\r\n\r\n" would be incorrect.
CURLOPT_CUSTOMREQUEST
执行 HTTP 请求时使用的自定义请求方法,而不是“GET”或“HEAD”。这对于执行“删除”或其他更模糊的 HTTP 请求很有用。有效值是“GET”、“POST”、“CONNECT”等;即不要在此处输入整个 HTTP 请求行。例如,输入“GET /index.html HTTP/1.0\r\n\r\n”将是不正确的。
The parameter for CURLOPT_CUSTOMREQUEST
should be simply POST
and you can use CURLOPT_HTTPHEADER
to set headers.
for 的参数CURLOPT_CUSTOMREQUEST
应该很简单POST
,您可以使用它CURLOPT_HTTPHEADER
来设置标题。