PHP在PHP小提琴中工作但在浏览器中不工作

时间:2014-02-25 03:45:43

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

我的PHP在我的PHP Fiddle中工作,但是当我将其完全复制到Sublime 2时,将其保存为.php文档,并将其上传到我的服务器here on my website。我认为在JSON中存在的问题是,它不是正确解码信息而且始终是“无效ID”,但是如果你在小提琴中运行它总是给出至少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);  
?>

更新:已启用错误报告。

Warning: file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /hermes/bosoraweb186/b1303/ipg.joshiefishbeincom/fi/namebook4.php on line 11
Warning: file_get_contents(https://graph.facebook.com/337800127/): failed to open stream: no suitable wrapper could be found in /hermes/bosoraweb186/b1303/ipg.joshiefishbeincom/fi/namebook4.php on line 11

更新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"}

1 个答案:

答案 0 :(得分:1)

我对Graph API太熟悉了,但我得到了这个: 从小提琴 我的本地服务器的.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);