Search code examples
phpjsoncurlfile-get-contents

PHP - File_get_contents doesn't detect CURL responce


I've my script here:

<?php

$auth_token = "privateprivateprivate";
$curl = curl_init("https://api-sandbox.coingate.com/v2/orders/");

$headers = array();
$headers[] = "Authorization: Token " .$auth_token;




$output = curl_exec($curl);
$output_json = file_get_contents($output);
$output_array = json_decode($output_json);

$message = $output_array["message"];
$reason  = $output_array["reason"];

echo $message;
echo $reason;
?>

What i'm trying to do is to make a call to "https://api-sandbox.coingate.com/v2/orders/" and to fetch info's using file_get_contents() function of PHP.

The results of that file is JSON so I do a basic decode but when I run the php scripts, it says: file_get_contents(): Filename cannot be empty in ...

This is my first attempt at working with curl, can I get some help please?

Thanks!


Solution

  • To return the requested data and save it to a variable, you can use the curl_setopt function with the CURLOPT_RETURNTRANSFER option:

    <?php
    
    $auth_token = "privateprivateprivate";
    $headers = [
        "Authorization: Token " .$auth_token,
    ];
    
    $curl = curl_init("https://api-sandbox.coingate.com/v2/orders/");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    
    $output = curl_exec($curl);
    $output_array = json_decode($output);
    
    var_dump($output, $output_array);
    

    To read more you can take a look into the PHP cURL documentation: http://de.php.net/manual/de/book.curl.php