doutan5724 2012-01-19 10:35
浏览 60
已采纳

file_get_contents无法使用批量请求从Facebook获取数据

file_get_contents not working for fetching fata from facebook using batch requests.Am using the code below:

  $url='https://graph.facebook.com/?batch=[{ "method": "POST", "relative_url":"method/fql.query?query=SELECT+first_name+from+user+where+uid=12345678"}]&  access_token=xxxxxxx&method=post';
 echo  $post = file_get_contents($url,true);
it produces 
    Warning: file_get_contents(graph.facebook.com/?batch=[{ "method": "POST", "relative_url": "method/fql.query?query=SELECT+first_name+from+user+where+uid=12345"}]&access_to‌ ​ken=xxxx&method=post): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /home/user/workspace/fslo/test.php on line 9
  • 写回答

1条回答 默认 最新

  • douhuan1950 2012-01-19 10:57
    关注

    I would say the most likely answer to this is that you need to pass the URL values through urlencode() - particularly the JSON string.

    Also, you should be POSTing the data.

    Try this code:

    NB: I presume you are building the URL from several variables. If you edit the question with your actual code, I will provide a solution using that code

    <?php
    
      $baseURL = 'https://graph.facebook.com/';
    
      $requestFields = array (
        'batch' => '[{"method":"POST","relative_url":"method/fql.query?query=SELECT+first_name+from+user+where+uid=12345678"}]',
        'access_to‌ken' => 'whatever'
      );
      $requestBody = http_build_query($requestFields);
    
      $opts = array(
        'http'=>array(
          'method' => 'POST',
          'header' => "Content-Type: application/x-www-form-urlencoded
    "
                    . "Content-Length: ".strlen($requestBody)."
    "
                    . "Connection: close
    ",
          'content' => $requestBody
        )
      );
    
      $context = stream_context_create($opts);
    
      $result = file_get_contents($baseURL, FALSE, $context);
    

    A "more standard" way to do this these days is with cURL:

    <?php
    
      $baseURL = 'https://graph.facebook.com/';
    
      $requestFields = array (
        'batch' => '[{"method":"POST","relative_url":"method/fql.query?query=SELECT+first_name+from+user+where+uid=12345678"}]',
        'access_to‌ken' => 'whatever'
      );
      $requestBody = http_build_query($requestFields);
    
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $baseURL);
      curl_setopt($ch, CURLOPT_POST, TRUE);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/x-www-form-urlencoded',
        'Content-Length: '.strlen($requestBody),
        'Connection: close'
      ));
    
      $post = curl_exec($ch);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题