Я просматриваю документацию для API VerMail, и они указывают, что мне нужно установить заголовки в "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>
Добавьте заголовки вручную, чтобы вы могли указать, что вы отправляете и хотите вернуть XML
Вам не нужно указывать URL-адрес xml.
Также поля postfield должны быть только 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);