我的 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);
?>
更新:打开错误报告。
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
UPDATE 2: now using cURL
This is my new code:
<?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:
我的错,只需从返回中删除 header 即可:
curl_setopt($ch, CURLOPT_HEADER, 0);
此外,请求无需 SSL
$username = "http://graph.facebook.com/" . $value . "/";
最后,您仍然需要解码 JSON:
$json = curl_exec($ch);
$json = json_decode($json, true);
关于javascript - PHP 可在 PHP Fiddle 中运行,但不能在浏览器中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22004140/