我正在查看VerMail API的文档,并指出我需要将标头设置为“application / x-www-form-urlencoded”,但我必须将数据作为XML发送..我知道这是如果我在数组中发送数据但是如何使用XML自动执行?
这是我到目前为止的代码:
$xmlcontent = "
<api>
<authentication>
<api_key>".$apiKey."</api_key>
<shared_secret>".$apiSecret."</shared_secret>
<response_type>xml</response_type>
</authentication>
<data>
<methodCall>
<methodname>legacy.message_stats</methodname>
<last>100</last>
</methodCall>
</data>
</api>
";
$xmlcontent = urlencode($xmlcontent);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "XML=".$xmlcontent);
$content = curl_exec($ch);
print_r($content);
这是我在运行源代码时看到的内容:
HTTP/1.1 200 OK
Date: Fri, 23 Jan 2015 20:17:52 GMT
Server: Apache
Cache-Control: max-age=18000
Expires: Sat, 24 Jan 2015 01:17:52 GMT
Content-Length: 595
Connection: close
Content-Type: text/xml;charset=utf-8
Set-Cookie: BIGipServerBH-gen-80=235023882.20480.0000; path=/
<?xml version="1.0" encoding="utf-8"?>
<methodResponse><item><error><![CDATA[1]]></error><responseText><![CDATA[ XML Error: Please verify the XML request is valid. For special characters please ensure you are using <![CDATA[ ]]]]><![CDATA[> blocks and url encoding data properly.]]></responseText><responseData><error><![CDATA[Not well-formed (invalid token) at line: 1]]></error><responseCode><![CDATA[425]]></responseCode></responseData><responseNum><![CDATA[1]]></responseNum><totalRequests><![CDATA[0]]></totalRequests><totalCompleted><![CDATA[0]]></totalCompleted></item></methodResponse>
答案 0 :(得分:1)
手动添加标题,以便指定您要发送并想要返回XML
您不应该对xml进行URLencode。
此外,postfields应该只是XML。不是XML = $ xmlcontent
$xmlcontent = "
<api>
<authentication>
<api_key>".$apiKey."</api_key>
<shared_secret>".$apiSecret."</shared_secret>
<response_type>xml</response_type>
</authentication>
<data>
<methodCall>
<methodname>legacy.message_stats</methodname>
<last>100</last>
</methodCall>
</data>
</api>
";
$xmlcontent = urlencode($xmlcontent);
$ch = curl_init();
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"Content-length: ".strlen($xmlcontent),
);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlcontent);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$content = curl_exec($ch);