【发布时间】: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