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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器