2014-02-25 8 views
1

내 PHP는 내 PHP Fiddle에서 작동하지만 정확히 2 부에 복사 한 후 .php 문서로 저장하고 내 서버 here on my website에 업로드하지 마십시오. JSON에 존재하는 것으로 생각되는 문제는 정보 권한을 디코딩하지 않고 항상 "잘못된 ID"이지만 피들에서 실행하면 항상 올바른 이름이 3-4 개 이상 제공됩니다. 하지만 크롬, 파이어 폭스, 사파리와 같은 브라우저에서는 절대 이름이 없습니다. 왜 이럴 수 있니?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"를 제공합니다. 그래서 나는 배열이 어떻게 생겼는지 볼 수 print_r($json);else을 전환하고 이것이 내가 가진 무엇 :

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'을 끄는 제외하고 .. 당신이 얘기하고 있는지? –

+0

@vlzvl 어디에서 사용할 수 있습니까? 피들? 브라우저에서? 나는 아무 경고도 얻지 않는가? 당신의 경고는 무엇입니까? – j0sh1e

+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; } 자신을 뒀다. 당신도 그렇게 처리 및 인쇄 "잘못된 ID"할 수있을 것입니다하지만

{ 
    "error": { 
     "message": "Unsupported get request.", 
     "type": "GraphMethodException", 
     "code": 100 
    } 
} 

: 때때로 요청이 잘 작동 file_get_contents하지만,을 (물론, 대부분의 시간에)이 같은 오류 JSON을 반환 보인다. 내 생각에 서버가 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을 사용하도록 설정하십시오.

내 나쁜, 단지에서 헤더를 제거합니다

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

EDIT (3) 당신은 (또한도 비활성화 될 수 있습니다) cURL 또는 fsockopen A는, 여기에 간단한 코드입니다 사용할 수 있습니다 반환 : 또한

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

공백이 있습니까? 또한 이러한 요청은 '시간 안에 끌리는'것이다. 어쩌면 최대 실행 시간도 소비됩니까? (편집 확인). –

+0

시간 초과되지 않고 즉시 50 개의 항목이 모두 유효하지 않게 렌더링됩니다. 심지어 10, 5, 1로 조정 했는데도 여전히 유효하지 않습니다. – j0sh1e

관련 문제