2014-02-25 35 views
1

我的PHP在我的PHP Fiddle中工作,但不是当我将它完全复制到Sublime 2中时,将它保存为.php文件,将其上传到我的服务器here on my website。我认为JSON存在的问题是它没有正确解码信息,并且始终是“无效的ID”,但是如果您在小提琴中运行,它总是会提供至少3-4个正确的名称。但是在Chrome,Firefox或Safari等任何浏览器中,我从来没有得到任何名字。为什么会这样呢?PHP在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 . "/"; 
    $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:现在使用的卷曲

这是我的新代码:

<?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”。所以我换了elseprint_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"} 
+0

作品对我来说,除了它得到了很多'warnings'的..是你在说什么? – 2014-02-25 04:12:25

+0

@vlzvl它在哪里适合你?在小提琴?在浏览器中?我没有收到警告?你有什么警告? – j0sh1e

+0

查看我的回答 – 2014-02-25 04:27:40

回答

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这样的:

​​

虽然你将能够处理太和打印“无效的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已禁用,因此您cann根本不使用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); 
+0

我刚刚检查过这个。只用最底层的代码来看看是否存在'file_get_contents'并且它确实存在。没有错误。 – j0sh1e

+0

你变空了吗?另外,这些请求是“拖时间”的。也许最大执行时间也消耗? (检查了它)。 – 2014-02-25 04:37:47

+0

它不超时,它立即呈现所有50个条目无效。我甚至将它调整到10和5和1,仍然无效。 – j0sh1e

相关问题