0

When I make a HTTP GET request, with curl, via php, I get this response:

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
    <apiStatus>ok</apiStatus>
    <matches>1</matches>
    <unit type="campaign">
        <id>42</id>
        <name>think_deep</name>
    </unit>
    <units>1</units>
</root>

When I try to get this output, to create a SimpleXMLElement, I get this erropr:

PHP Warning:  SimpleXMLElement::__construct(): Entity: line 1: parser error : Start tag expected, '<' not found in /home/simone/Development/dotadv/cxense/scripts/SearchForCampaign.php on line 35
PHP Warning:  SimpleXMLElement::__construct(): 1 in /home/simone/Development/dotadv/cxense/scripts/SearchForCampaign.php on line 35
PHP Warning:  SimpleXMLElement::__construct(): ^ in /home/simone/Development/dotadv/cxense/scripts/SearchForCampaign.php on line 35
PHP Fatal error:  Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/simone/Development/dotadv/cxense/scripts/SearchForCampaign.php:35
Stack trace:
#0 /home/simone/Development/dotadv/cxense/scripts/SearchForCampaign.php(35): SimpleXMLElement->__construct('1')
#1 {main} thrown in /home/simone/Development/dotadv/cxense/scripts/SearchForCampaign.php on line 35

With a var_dump() I discovered that the output is:

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
    <apiStatus>ok</apiStatus>
    <matches>1</matches>
    <unit type="campaign">
        <id>42</id>
        <name>think_deep</name>
    </unit>
    <units>1</units>
</root>
bool(true)
bool(true)

So, ... What kind of issue is this?

$curl = curl_init();
$url = 'http://stage.emediate.eu' . $request->uri();
curl_setopt_array($curl, [
    CURLOPT_URL => $url,
]);

$resp = curl_exec($curl);
var_dump($resp, true);
sensorario
  • 20,262
  • 30
  • 97
  • 159

1 Answers1

1

Because you do not set option CURLOPT_RETURNTRANSFER, curl_exec return true or false as result of request. So, $resp gets true, which we can see in the output of var_dump($resp, true);

bool(true)
bool(true)

To receive the xml as result change your code to

curl_setopt_array($curl, [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => 1
]);
splash58
  • 26,043
  • 3
  • 22
  • 34