Ioannis Tavernarakis Ioannis Tavernarakis - 6 months ago 25
PHP Question

Curl Function Return Undefined Variable

My problem is that the code i post is returning Undefined variable $responseXML

(curl.php)

<?php
function curl_seasson($url) {
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$responseXML = curl_exec($c);
curl_close($c);
}
?>


(index.php)

<?php
$url = "site.xml.format";

require_once("curl.php");

curl_seasson($url);
$xml = simplexml_load_string($responseXML);

\\
\\
?>

Answer

You need to return the xml data which you get from your curl call.

curl.php:

<?php
    function curl_seasson($url) {
        $c = curl_init($url);
        curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
        $responseXML = curl_exec($c);
        curl_close($c);

        return $responseXML;
    }
?>

index.php:

<?php
    $url = "site.xml.format";

    require_once("curl.php");

    $responseXML = curl_seasson($url);
    $xml = simplexml_load_string($responseXML);

?>