Mikelo
Mikelo

Reputation: 281

No output when using cUrl

I'm trying to recieve the contents of a json file here, but when I wanted to echo the output ($json) it didn't gave me any. I had a look at some other questions involving cUrl here on stackoverflow and used the cUrl setup that was given in the answers so it should be working fine. Am I doing something wrong? Here's my code:

$url = 'http://steamcommunity.com/profiles/<your 64-bit steam ID>/inventory/json/730/2';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$json = json_decode($output, true);
print_r($json);

Upvotes: 5

Views: 2687

Answers (1)

Vika Marquez
Vika Marquez

Reputation: 353

Add this after $output = curl_exec($ch); line :

$info = curl_getinfo($ch);
echo "<pre>";   
print_r($info);
echo "</pre>";

and print here what you see in browser, please. Without this information people can't help you.

I run your code and it's work fine. I have HTTP answer code 200 and page loaded successfully. Page said: "The specified profile could not be found" (it's ok, because I have not a key).

My $info printing:

Array
(
    [url] => http://steamcommunity.com/profiles//inventory/json/730/2
    [content_type] => text/html; charset=UTF-8
    [http_code] => 200
    [header_size] => 927
    [request_size] => 109
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.561
    [namelookup_time] => 0.062
    [connect_time] => 0.109
    [pretransfer_time] => 0.109
    [size_upload] => 0
    [size_download] => 18266
    [speed_download] => 32559
    [speed_upload] => 0
    [download_content_length] => 18266
    [upload_content_length] => -1
    [starttransfer_time] => 0.561
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 2.17.165.89
    [certinfo] => Array
        (
        )

    [primary_port] => 80
    [local_ip] => 192.168.1.33
    [local_port] => 53366
)    

All working perfectly. Maybe you have a poor connection to steamcommunity.com? If you'll receive [http_code] => 0 it will mean yes.

And what you see if add this:

print_r($output);

Maybe page loaded, but not as JSON? If yes, your $json should be empty, it's absolutelly ok.

UPDATE AFTER YOUR COMMENT:

You received 302 HTTP-code and URL for redirect. You need go to this URL and you'll received JSON. Full code:

$url = 'http://steamcommunity.com/profiles/<your 64-bit steam ID>/inventory/json/730/2';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_setopt($ch, CURLOPT_URL, $info["redirect_url"]); // Set new URL
$output = curl_exec($ch); // Go to new URL
$json = json_decode($output, true); // Your JSON here, I checked it
print_r($json); // Print JSON

Upvotes: 4

Related Questions