PHP在PHP Fiddle中工作,但在浏览器中不工作



我的PHP在我的PHP Fiddle中工作,但当我将其完全复制到Sublime 2中时,请将其保存为.PHP文档,并将其上传到我网站上的服务器。我认为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在服务器配置中被第11行/hermes/bosraweb186/b1303/ipg.johiefishbein/fi/namebook4.php中的allow_url_fopen=0禁用警告:file_get_contents(https://graph.facebook.com/337800127/):无法打开流:在第11行的/hermes/bosraweb186/b1303/ipg.johiefishbein/fi/namebook4.php中找不到合适的包装器

UPDATE 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"}

我对Graph API太不熟悉了,但我知道:从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。你可能想使用cURL或fsockopen,这里有一个简单的代码(也可能被禁用)

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);

最新更新