【问题标题】:PHP Works in PHP Fiddle but not in BrowserPHP 在 PHP Fiddle 中工作,但在浏览器中不工作
【发布时间】:2014-03-27 01:58:42
【问题描述】:

我的 PHP 在我的 PHP Fiddle 中工作,但当我将其完全复制到 Sublime 2 中时,将其保存为 .php 文档,将其上传到我的服务器 here on my website。我认为 JSON 中存在的问题,它没有正确解码信息并且总是“无效 ID”,但是如果你在 Fiddle 中运行它总是给出至少 3-4 个正确的名称。但是在使用 Chrome、Firefox 或 Safari 等任何浏览器时,我从来没有得到任何名字。为什么会这样?

<?php
function gen_pix($min, $max, $quantity) {
$numbers = range($min, $max);
shuffle($numbers);
$x_arr = array_slice($numbers, 0, $quantity);
foreach ($x_arr as $key => $value) {
    $username = "https://graph.facebook.com/" . $value . "/";
    $json = json_decode(file_get_contents($username), true);
    if (!isset($json['name'])) {
        echo "Invalid ID<br />";
    }
    else {
        echo $json["name"]. '<br />';
    } 
  }
}

$x = 337800042;
$y = 337800382;
$z = 50;

gen_pix($x,$y,$z);  
?>

更新:打开错误报告。

警告:file_get_contents(): https:// wrapper is disabled in server configuration by allow_url_fopen=0 in /hermes/bosoraweb186/b1303/ipg.joshiefishbeincom/fi/namebook4.php on line 11 警告:file_get_contents(https://graph.facebook.com/337800127/):无法打开流:在第 11 行的 /hermes/bosoraweb186/b1303/ipg.joshiefishbeincom/fi/namebook4.php 中找不到合适的包装器

更新 2:现在使用 cURL

这是我的新代码:

<?php
ini_set("display_errors",1); 
error_reporting(E_ALL);

function gen_pix($min, $max, $quantity) {
$numbers = range($min, $max);
shuffle($numbers);
$x_arr = array_slice($numbers, 0, $quantity);
foreach ($x_arr as $key => $value) {
    $username = "https://graph.facebook.com/" . $value . "/";

    if (!function_exists("curl_init")){
       die('Sorry cURL is not installed!');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $username); 
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);  // 60 seconds timeout
    $json = curl_exec($ch);
    curl_close($ch); 

    if (!isset($json['name'])) {
        print("error");
    }
    else {
        print($json["name"]). '<br />';
    } 
  }
}

$x = 337800042;
$y = 337800382;
$z = 10;

gen_pix($x,$y,$z);  
?>

现在每行只给我“H”。所以我把else 换成了print_r($json); 看看数组是什么样子的,这就是我得到的:

HTTP/1.1 200 OK Access-Control-Allow-Origin: * Cache-Control: private, no-cache, no-store, must-revalidate Content-Type: application/json; charset=UTF-8 ETag: "f07656bdb736f1a09e1aa2bb16ecce2b3b1f483e" Expires: Sat, 01 Jan 2000 00:00:00 GMT Pragma: no-cache X-FB-Rev: 1135358 X-FB-Debug: Ha05GqDUPxzl4bA3x9xreOZCveNsf8QiOGExgPU9p6c= Date: Tue, 25 Feb 2014 05:12:49 GMT Connection: keep-alive Content-Length: 148 {"id":"337800051","name":"Irem Muftuoglu","first_name":"Irem","last_name":"Muftuoglu","gender":"female","locale":"nb_NO","username":"iremmuftuoglu"}

【问题讨论】:

  • 对我也有用,只是它得到了很多 warnings.. 你在说吗?
  • @vlzvl 它在哪里对您有用?在小提琴?在浏览器中?我没有收到警告?你有什么警告?

标签: javascript php json facebook-graph-api curl


【解决方案1】:

我对@9​​87654324@ 太不熟悉了,但我明白了: 从 Fiddle 我的 本地服务器的 .php 调用它,我得到:

<?php
function gen_pix($min, $max, $quantity) {
$numbers = range($min, $max);
shuffle($numbers);
$x_arr = array_slice($numbers, 0, $quantity);
foreach ($x_arr as $key => $value) {
   $username = "https://graph.facebook.com/" . $value . "/";
      $data = file_get_contents($username);

      // if ($data===FALSE) { continue; }

      $json = json_decode($data, true);

      if (!isset($json['name'])) {
         echo "Invalid ID<br />";
      }
      else {
         echo $json["name"]. '<br />';
      } 
   }
}

$x = 337800042;
$y = 337800382;
$z = 50;

gen_pix($x,$y,$z);  
?>

我自己写了if ($data===FALSE) { continue; } 这条线,虽然它并没有解决什么问题。 file_get_contents 请求似乎运行良好,但有时(嗯,在大多数情况下)返回如下错误 JSON:

{
   "error": {
      "message": "Unsupported get request.",
      "type": "GraphMethodException",
      "code": 100
   }
}

虽然您也可以处理它并打印“无效 ID”。 我的第一个想法是,如果您的服务器完全支持file_get_contents,您的error_reporting 是否开启?

ini_set("display_errors",1); 
error_reporting(E_ALL);
...
if (function_exists("file_get_contents")) {
   file_get_contents("...")
}

编辑:

只是为了验证最大执行时间.. 您能否以某种方式验证通话是否比这更长:

echo ini_get("max_execution_time");

编辑 2:

所以allow_url_fopen 被禁用,因此您根本无法使用file_get_contents,或启用allow_url_fopen。 您可能想使用cURLfsockopen,这是一个简单的代码(也可能会被禁用)

if (!function_exists("curl_init")){
   echo "Sorry cURL is not supported either!";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "THE_URL"); 
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60);  // 60 seconds timeout
$json = curl_exec($ch);
curl_close($ch); 

编辑 3:

我的错,只需从返回中删除标题:

curl_setopt($ch, CURLOPT_HEADER, 0);

另外,不使用 SSL 的请求

$username = "http://graph.facebook.com/" . $value . "/";

最后,你还需要对 JSON 进行解码:

$json = curl_exec($ch);
$json = json_decode($json, true);

【讨论】:

  • 我刚刚检查了这个。只需最底部的代码即可查看file_get_contents 是否存在并且确实存在。没有错误。
  • 你空白了?此外,这些请求是dragging in time。也许最大执行时间也被消耗了? (检查编辑)。
  • 它不会超时,它会立即将所有 50 个条目呈现为无效。我什至已经调整到10和5和1,仍然无效。
  • 这怎么可能?我花了将近 40 秒来调用所有请求 :) 看来你需要 print_r($json) 看看它有什么。似乎根本没有请求,但你没有收到错误报告。
  • 等等,我的错,错误实际上并没有出现。我有错误。检查上面的原始帖子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-23
  • 1970-01-01
  • 1970-01-01
  • 2014-06-27
  • 1970-01-01
  • 2021-07-14
相关资源
最近更新 更多