0

I have some code which is quite simple, it looks to query an API using CURL and return the json response.

Here is the code:

<?php

...[VARIABLES]...

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$result = curl_exec($ch);
var_dump($result);

?>

Here is the response:

string(310) "{"totalPages":0,"firstPage":true,"lastPage":true,"numberOfElements":0,"number":0,"totalElements":0,"columns":{"columnIds":["metrics/visits:::0","metrics/visitors:::2","metrics/timespentvisit:::4"]},"summaryData":{"totals":[2740.0,1384.0,241.4753313696613]},"oberonRequestXML":[null],"oberonResponseXML":[null]}"

I'm struggling to understand why the string(310) is being shown at the front of the json response, since I am expecting a JSON response only.

6
  • 6
    Because you're using var_dump. What do you expect? php.net/var_dump Commented Feb 22, 2019 at 10:42
  • Replace var_dump() with echo Commented Feb 22, 2019 at 10:43
  • 1
    Possible duplicate of Remove String Before Json Response Body Php Commented Feb 22, 2019 at 10:43
  • @JonStirling I was expecting a raw JSON. It sounds like that is the issue then. Should I use something like echo json_encode($result); ? Commented Feb 22, 2019 at 10:44
  • @Jimmy Looks like it's already json, so just echo $result I guess. Commented Feb 22, 2019 at 10:45

1 Answer 1

1

use echo to print response

<?php

    ...[VARIABLES]...

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $result = curl_exec($ch);
    echo $result;

    ?>
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.