1
    $url = "myurl";

    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    $passwordStr = "$this->username:$this->password"; 
    curl_setopt($ch, CURLOPT_USERPWD, $passwordStr);

    curl_setopt($ch, CURLOPT_HTTPHEADER,
        array("Content-type: application/atom+xml"));

    $successCode = 200;

    $result = curl_exec($ch); 

curl_exec obtains xml data from myurl, as expected, but simply echoes it in the output buffer. The value of $return is a simple boolean. Instead, I would like it to return the obtained xml data as an array or any other php object that I can process (without having to do any cumbersome ob_start output buffering). Is this possible or is there any workaround?

2 Answers 2

1

You need to set curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

From http://php.net/manual/en/function.curl-setopt.php:

CURLOPT_RETURNTRANSFER TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

Sign up to request clarification or add additional context in comments.

Comments

1

you will need to use CURLOPT_RETURNTRANSFER option to return your response rather than echoing it.

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

after that, you may need to use SimpleXMLElement to parse your xml into an array :

$result = curl_exec($ch);
$resultArray = new SimpleXMLElement($result);

SimpleXMLElement will return an instance of SimpleXMLElement object.


another option of parsing your xml into an array by using xml_parse_into_struct :

$result = curl_exec($ch);
$parser = xml_parser_create();
xml_parse_into_struct($parse, $result, $values, $indexes);
xml_parser_free($parser);

// then you can access your array values using $values,$indexes variables
print_r($values);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.