0

I'm created a simple PHP curl client that call a external webservice. This webservice return the XML.

<?php
// Method: POST, PUT, GET etc
// Data: array("param" => "value") ==> index.php?param=value
function CallAPI($method, $url, $data = false)
{
    $curl = curl_init();
    switch ($method)
    {
        case "POST":
            curl_setopt($curl, CURLOPT_POST, 1);
            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
        case "PUT":
            curl_setopt($curl, CURLOPT_PUT, 1);
            break;
        default:
            if ($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
    }
    // Optional Authentication:
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curl);
    curl_close($curl);

    return $result;
}
$response = CallAPI('GET','https://s1-en.ogame.gameforge.com/api/universes.xml');
var_dump($response);
//print string(1121) " "
?>

But it always gives me back the wrong value. I print string(1121) " ".

How can I get the call data? (line 41 in the test.php).

1 Answer 1

2

This is because you are viewing the result in browser and the browser parses the xml as valid html tag. So you get empty result.

Its not your code issue. you can press ctrl + u (view source) in browser to see the actual result.

Or you can edit your var_dump to echo htmlentities($response); on line 41.

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.