duanlie2709 2011-03-26 18:21
浏览 135

将file_get_contents用于循环中的不同URL似乎会破坏其余的代码

I am currently trying to fetch some facebook data, which I then want to access in Javascript. Specifically, I am trying to access some characteristics of the user's friends. So I am getting the user's friend list using file_get_contents to his graph API URL. This provides me with an array of friend ids.

As I need a characteristic from each friend, I am doing:

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

Having this code piece seems to break the javascript code, as none of my alert instructions are ran.

Also, inserting a break after the if, so that only one file_get_contents is done, seems to make the code runnable (but I obviously need to go through all of the friends).

How can I solve this?

I would use jQuery or xmlHttpRequest to do the HTTP GET, but somehow I always seem to get back a status code of 0, with an empty response.

Edit: Here is the JS code:

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

I should've also added that this is being done on a page embedded into facebook using the canvas feature.

  • 写回答

2条回答 默认 最新

  • dongsechuan0535 2011-03-26 18:30
    关注

    Try...

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

    Maybe FGC is disabled but you don't get any notifications/warnings.

    Code from comment:

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

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!