通过cURL发送XML的功能

时间:2012-02-03 19:58:24

标签: php curl

这是我的函数,它应该将XML发送到配置中指定的服务器,然后将响应(即XML)转换为数组..

public function xmlResponse($data) {
    // sends the request to Atheme's XMLRPC interface via cURL
    $request = curl_init();
    curl_setopt($request, CURLOPT_URL, $this->atheme_host);
    curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($request, CURLOPT_TIMEOUT, 6);
    curl_setopt($request, CURLOPT_POSTFIELDS, $data);
    curl_setopt($request, CURLINFO_HEADER_OUT, true);
    curl_setopt($request, CURLOPT_HTTPHEADER, array('Connection: close', 'Content-Type: text/xml'));
    $response = curl_exec($request);
    curl_close($request);

    // converts recieved XML into a PHP array and returns
    return json_decode(json_encode((array) simplexml_load_string($response)), 1);
}

但是,当我执行var_dump时,返回False。我不确定问题是什么,现在已经诊断了一段时间了。如果有人能够指出问题和解决方案,将不胜感激。我认为cURL有问题。

谢谢!

1 个答案:

答案 0 :(得分:2)

不熟悉Atheme或其API,我不确定这是否有帮助,但是一些接受XML的API工作如下:

$header = "Connection: close\r\n";
$header .= "Content-Type: text/xml\r\n\r\n";
$header .= $data;

$request = curl_init();
curl_setopt($request, CURLOPT_URL, $this->atheme_host);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_TIMEOUT, 6);
curl_setopt($request, CURLOPT_HEADER, false);
curl_setopt($request, CURLOPT_CUSTOMREQUEST, $header);
$response = curl_exec($request);
curl_close($request);