【问题标题】:file_get_contents(http://roblox.plus:2052/limiteds): failed to open stream: Connection refusedfile_get_contents(http://roblox.plus:2052/limiteds):无法打开流:连接被拒绝
【发布时间】:2016-06-18 16:03:26
【问题描述】:

当我运行下面的代码时,我得到了错误:

file_get_contents(http://roblox.plus:2052/limiteds):打开失败 流:连接被拒绝

$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>";
}

这是为什么?我可以通过浏览器正常访问该网站。我也试过PHP Fiddle上的代码。

在 cmets 之后,我尝试使用 cURL - 但是没有显示任何结果。

$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>";
}

【问题讨论】:

  • 可能的解决方案:stackoverflow.com/a/697514/4220475
  • 是的,最好使用 cURL:forums.phpfreaks.com/topic/…
  • @bucketman 我尝试过使用 cURL。请查看我更新的帖子
  • 你能从命令行做 curl 吗? curl http://roblox.plus:2052/limiteds - 如果您发送的请求过多,他们可能会阻止您的服务器。
  • @bobjomes 许多虚拟主机(甚至共享)允许您通过 ssh 登录,或者至少在他们的网络界面中为您提供命令行。

标签: php json file-get-contents


【解决方案1】:

出于安全原因,许多主机会阻止您从远程 URL 加载文件。最好使用 CURL 来下载文件的内容。

我尝试如下,它工作正常。

这个 file_get_contents 方法:

<?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>';    
    }
?>

CURL 方法中的这个:

<?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>';
    }
?>

或者你可以打开流

<?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>';    
    }
?>

这 3 种方法都可以正常工作

样本输出

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

【讨论】:

  • 您好伊万,感谢您的回复。您给我的第一个示例仍然显示与我最初收到的相同的错误。第二个有一个未知变量$jsond,第三个也显示相同的错误消息。
  • 在第二个示例中将 foreach ($jsond['data'] as $vals) { 更改为 foreach ($decode['data'] as $vals) { 并尝试一下
  • 我已按照您的建议进行了更改,现在显示为 Invalid argument supplied for foreach() -- at line 12
  • 我更新了它,复制粘贴第二个例子,然后再试一次。
  • 还是同样的问题
猜你喜欢
  • 1970-01-01
  • 2013-03-27
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-14
  • 2018-12-14
  • 2011-12-19
相关资源
最近更新 更多