5

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);
4
  • I would guess to view that url normally you would have to be logged in? Commented Jan 20, 2016 at 18:09
  • No, you only have to set certain privacy settings to public on your Steam account. And as I'm trying this with my own account I'm 100% certain that this is the case. Commented Jan 20, 2016 at 18:11
  • I would try adding a useragent to the curl options, that is quite a usual thing to check Commented Jan 20, 2016 at 18:13
  • That didn't help unfortunately... Commented Jan 20, 2016 at 19:04

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your reply! This is my result of the $info: pastebin.com/XKr6qcy0 So http_code gives me a 302. Also, when printing the $output it gives me this: ‹ÿÿ, 3 weird chars..
Works like a charm :) Thanks a lot!

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.