【问题标题】:Using file_get_contents to different URLs in a loop seems to break the rest of the code在循环中对不同的 URL 使用 file_get_contents 似乎会破坏其余代码
【发布时间】:2011-03-26 18:21:07
【问题描述】:

我目前正在尝试获取一些 facebook 数据,然后我想在 Javascript 中访问这些数据。具体来说,我正在尝试访问用户朋友的一些特征。 因此,我使用 file_get_contents 将用户的朋友列表添加到他的图形 API URL。 这为我提供了一组朋友 ID。

由于我需要每个朋友的特点,我正在这样做:

foreach($dataarray as $friend) {
$friendurl = "https://graph.facebook.com/".$friend->id."?access_token=".$token."";
$fdata = json_decode(file_get_contents($friendurl));
if($fdata->gender == "male") {
 array_push($fulldata, $fdata->name);
    }
}

拥有这段代码似乎破坏了 javascript 代码,因为我的警报指令都没有运行。

另外,在 if 之后插入一个 break,这样只完成一个 file_get_contents,似乎可以使代码可以运行(但我显然需要通过所有的朋友)。

我该如何解决这个问题?

我会使用 jQuery 或 xmlHttpRequest 来执行 HTTP GET,但不知何故,我似乎总是返回 0 的状态码,响应为空。

编辑: 这是JS代码:

<script type="text/javascript">
        function initialize() {

                alert('Test1');
                <?php

                $fulldata = array();

                $data = $result->data;

                foreach($data as $friend) {
                    $friendurl = "https://graph.facebook.com/".$friend->id."?access_token=".$token."";
                    //echo("alert(\"".$friendurl."\");");
                    $fdata = json_decode(file_get_contents($friendurl));
                    if($fdata->hometown->name) {
                        array_push($fulldata, $fdata->hometown->name);
                    }
                }
                echo ("alert(\"".count($fulldata)."\")");
                ?>

                }
         </script>

我还应该补充一点,这是在使用画布功能嵌入到 facebook 的页面上完成的。

【问题讨论】:

  • 这里没有显示警报的JS。
  • 我编辑了这个问题,以防你真的不明白我的意思。
  • 你贴的代码是纯PHP的..把坏掉的JS代码贴出来,我们会更聪明的。
  • 因为你是用php输出html,所以在你的浏览器中查看源代码,看看它正在生成什么html(标签)并粘贴到这里。

标签: php jquery loops xmlhttprequest file-get-contents


【解决方案1】:

试试……

function curl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    return curl_exec($ch);
    curl_close ($ch);
}

foreach($dataarray as $friend){
    $friendurl = "https://graph.facebook.com/".$friend->id."?access_token=".$token."";
    $fdata = json_decode(curl($friendurl));
    if($fdata->gender == "male"){
            array_push($fulldata, $fdata->name);
        }
}

也许 FGC 已禁用,但您没有收到任何通知/警告。

评论代码:

error_reporting(E_ALL); ini_set("display_errors", 1);

【讨论】:

  • 不幸的是,我对您的解决方案不满意。但是我认为我的服务器没有 curl,也许这就是它无法正常工作的原因(我测试了您发布的 curl 函数中的代码,在 init 之前和之后调用了 echo,并且只有在 init 成功之前的那个) .我也没有收到任何错误消息。
  • 添加error_reporting(E_ALL); ini_set("display_errors", 1);在您的 PHP 之上,如果未安装 cURL,它必须显示消息。还可以尝试将 HTTPS 更改为 HTTP,因为没有 cURL,也许也没有 SSL 支持。
  • 感谢您的提示。问题确实是没有安装 curl(调用未定义的函数 curl_init())。关于将 https 更改为 http,我做不到,因为当您需要传递访问令牌时,facebook 需要 https。
  • 是的,好久没用FB了。尝试使用file_get_contents,我认为问题在于SSL(HTTPS)支持,可能会报错。
【解决方案2】:

请注意,出于安全原因,您正在执行禁止的跨域 AJAX 调用。 您可以在服务器上进行api调用并将数据回显到客户端JS,或者您可以构建一个php代理返回Graph API调用的结果(因为代理在您自己的服务器上,它们在同一个域中)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    • 1970-01-01
    相关资源
    最近更新 更多