2

When I run the code below, I get the error:

file_get_contents(http://roblox.plus:2052/limiteds): failed to open stream: Connection refused

$file = file_get_contents('http://roblox.plus:2052/limiteds');
$decode = json_decode($file, false);

foreach($decode AS $person) {
    echo $person->name . ": " . $person->lowestPrice . "<br><br>";
}

Why is this? I can access the website fine through my browser. I also tried the code on PHP Fiddle.

Following the comments, I have attempted to use cURL - no results are displayed though.

$url = 'http://roblox.plus:2052/limiteds';

//  Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);

// Decode
$decode = json_decode($result, false);


foreach($decode AS $person) {
    echo $person->name . ": " . $person->lowestPrice . "<br><br>";
}
bob jomes
  • 271
  • 1
  • 4
  • 13
  • 1
    possible solution: http://stackoverflow.com/a/697514/4220475 – bucketman Jun 18 '16 at 16:10
  • 1
    Yes, better use cURL: http://forums.phpfreaks.com/topic/162357-solved-file-get-contents-cant-access-a-url-that-a-browser-can/ – KIKO Software Jun 18 '16 at 16:18
  • @bucketman I have tried using cURL. Please check my updated post – bob jomes Jun 18 '16 at 16:27
  • Can you do curl from the command line? `curl http://roblox.plus:2052/limiteds` - they may have blocked your server if you've been sending too many requests. – JimL Jun 18 '16 at 16:35
  • @bobjomes many webhosts (even shared) allow you to ssh in, or at the very least give you a command line in their web interface. – JimL Jun 18 '16 at 16:38

1 Answers1

1

A lot of hosts will prevent you from loading files from remote URLs for security reasons. It's better to use CURL to download the contents of the file.

I try it like as below and it works fine.

This file_get_contents method :

<?PHP
    $file = file_get_contents('http://roblox.plus:2052/limiteds');
    $jsond = json_decode($file, true);

    foreach ($jsond['data'] as $vals) {
        echo $name = $vals["name"].' ';
        echo $lowp = $vals["lowestPrice"].'<br>';    
    }
?>

This in CURL method:

<?PHP
    $url = 'http://roblox.plus:2052/limiteds';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);
    $result=curl_exec($ch);
    curl_close($ch);

    $decode = json_decode($result, true);

    foreach ($decode['data'] as $vals) {
        echo $name = $vals["name"].' ';
        echo $lowp = $vals["lowestPrice"].'<br>';
    }
?>

Or you can open stream

<?php
    // Create a stream
    $opts = array(
            'http'=>array(
                    'method'=>"GET",
                    'header'=>"Accept-language: en\r\n" .
                            "Cookie: foo=bar\r\n"
            )
    );

    $context = stream_context_create($opts);

    // Open the file using the HTTP headers set above
    $file = file_get_contents('http://roblox.plus:2052/limiteds', false, $context);
    $jsond = json_decode($file, true);

    foreach ($jsond['data'] as $vals) {
       echo $name = $vals["name"].' ';
       echo $lowp = $vals["lowestPrice"].'<br>';    
    }
?>

All of this 3 methods working fine

Sample output

Red Baseball Cap 94
Classic ROBLOX Viking Helm 825
The Classic ROBLOX Fedora 30000
Domino Crown 4300000
Princess Hat 585
The Agonizingly Ugly Yellow Baseball Cap 1398
Jester's Cap 1355
Flag 699999
ROBLOX Classic: Wizard's Hat 599
Tornado Hat 1200
Target Hat 345
JJ5x5's White Top Hat 37142
Bucket 3899
Got Milk Visor 2497
Police Sergeants Cap 6000
Ivan Barayev
  • 2,035
  • 5
  • 24
  • 30