0

I created a php script to interact with an API to fetch balance of my account but its not working so I want to see the entire raw curl response with all headers, title and body. How can I achieve this? I'm very new to php so kindly consider this before answering.

<?php
  
$url = 'https://myapi.com';

$curl = curl_init($url . '?' . http_build_query($query_params));


$params =array();
$secret = "secret";
$post_data = '{}' ;

$checksum = build_checksum($params, $secret, $t, $r,$post_data);


curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl,CURLOPT_HTTP_CONTENT_DECODING, false);

curl_setopt($curl, CURLOPT_HTTPHEADER, [
    'X-API-CODE:"api_code"',
    "X-CHECKSUM: {$checksum}",
    'Content-Type: application/json'
]);

$response = curl_exec($curl);

curl_close($curl);

$data =json_decode($response);


}
?>

<!DOCTYPE html>
<html lang="en">
<body>
    <h2>My wallet</h2>
<br>
<br>
<form method="post">
        <input type="submit" name="balance" value ="Check Balance"/>
</form>
<br>
<?php
          
    echo "<h3>Your balance is: </h3>".$data->balance;
    echo $data;         
  
  ?>
</div>




</body>
</html>

Subo
  • 29
  • 3
  • Hi. It doesn't work doesn't mean much. Do you have an error? what is not working? what is the input content?... – Monnomcjo Jul 04 '22 at 07:54
  • 3
    First thing you should do, is check what `$response` actually contains - _before_ you try to decode it as JSON. – CBroe Jul 04 '22 at 08:00
  • Does this answer your question? [Can PHP cURL retrieve response headers AND body in a single request?](https://stackoverflow.com/questions/9183178/can-php-curl-retrieve-response-headers-and-body-in-a-single-request) – M. Eriksson Jul 04 '22 at 08:04
  • _"response with all headers, title and body"_ - "title" isn't really a thing in a HTTP request/response. You can get the headers and the body (which where you might have a ``-tag if it's HTML, but that has no special meaning in a HTTP request/response). – M. Eriksson Jul 04 '22 at 08:06
  • 2
    I'd do a `var_dump($response)` right after the curl_close, to see what's actually returned from the API – Daniel Setréus Jul 04 '22 at 08:32
  • @Monnomcjo yes the api responds forbidden error so I want to see what actually generates that error. Is it my api code, secret or something else that's why I want to see the complete curl response. – Subo Jul 04 '22 at 10:30
  • @CBroe how can I do that? – Subo Jul 04 '22 at 10:30
  • `var_dump($response);` – CBroe Jul 04 '22 at 10:31
  • A Forbidden error may mean that the authenticated user does not have permissions to execute the request or is not authenticated at all. Every API reports errors differently. Can you say for certain authentication and permissions are working and setup correctly? You mention your secret key is in question. Hopefully, there's something in `var_dump($response)` that gives you a clue. I'd lean towards authentication issues on 4XX responses. – CChoma Jul 07 '22 at 15:06

0 Answers0