2

Here's my function which should send XML to the server specified in the config, and then convert the response (which is XML) to an array..

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);
}

However, this returns False when I do a var_dump. I'm not sure what the problem is and have been diagnosing it for a while now. Would be grateful if someone could point out the problem and a solution. It's something wrong with cURL I believe.

Thanks!

12
  • Show a var_dump of $data, it may not be a proper post string. Commented Feb 3, 2012 at 20:01
  • ` <?xml version="1.0"?> <methodCall><methodName>atheme.login</methodName><params><param><value><string>Sample Username</string></value></param><param><value><string>password123</string></value></param><param><value><string>123.456.78.94</string></value></param></params></methodCall>` This is the XML being sent. Commented Feb 3, 2012 at 20:03
  • Yes, you need to urlencode that data when you pass it as the value for CURLOPT_POSTFIELDS. Commented Feb 3, 2012 at 20:09
  • So curl_setopt($request, CURLOPT_POSTFIELDS, urlencode($data)); would work? Commented Feb 3, 2012 at 20:10
  • You should have more luck doing that yes, if you pass a string to CURLOPT_POSTFIELDS, it must be urlencoded. Commented Feb 3, 2012 at 20:11

1 Answer 1

2

Not being familiar with Atheme or its API, I'm not sure if this will help or not, but some APIs that accept XML work like this:

$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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.