2017-01-11 150 views
1

我想不捲曲,但與頭髮送GET請求。 問題是什麼?發送GET請求,而不捲曲

這裏是代碼我唧唧歪歪唧唧歪歪喇嘛:

private function getRequest($url, $data){ 
    $uri = $this->api_url . $url; 
    $content = http_build_query($data); 
    $content = urldecode($content); 

    $options = array(
     'http' => array(
      'header' => "Content-type: application/x-www-form-urlencoded\r\n" . 
          "Authorization : Bearer " . $this->access_token . "\r\n", 
      'method' => 'GET', 
      'content' => $content,//$data = ['key' => 'value'] 
     ), 
    ); 

    $context = stream_context_create($options); 
    var_dump($context); //resource(70) of type (stream-context) 
    if (fopen($uri, 'r', false, $context)) //NO 
     echo "yes"; 
    else 
     echo "no"; 
    fpassthru($fp); //empty 

} 

這裏是$選項返回值:

Array 
(
    [http] => Array 
     (
      [header] => Content-type: application/x-www-form-urlencoded 
Authorization : Bearer kEQn2I02JOegz6gD4 

      [method] => GET 
      [content] => address=%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0 //cyrillic, with ENG doesn't work anyway 
     ) 

) 
+0

你試過在這裏找到了建議: http://stackoverflow.com/questions/8596311/how-to-make -a-請求後無捲曲 – JavaBoy

+0

@JavaBoy'file_get_contents'不起作用 –

回答

0

您「授權」後,必須在頭文件中的空間,試試這個之一:

private function getRequest($url, $data){ 
    $uri = $this->api_url . $url; 

    $options = array(
     'http' => array(
      'header' => "Content-type: application/x-www-form-urlencoded\r\n" . 
         "Authorization: Bearer " . $this->access_token . "\r\n", 
      'method' => 'GET', 
      'content' => http_build_query($data),//$data = ['key' => 'value'] 
     ), 
    ); 

    $context = stream_context_create($options); 
    var_dump($context); //resource(70) of type (stream-context) 
    if (fopen($uri, 'r', false, $context)) //NO 
     echo "yes"; 
    else 
     echo "no"; 
    fpassthru($fp); //empty 

} 
+0

沒了,並與捲曲'授權:Bearer'做工不錯 –

+0

我測試過它在我的本地服務器上,並已得到了一個錯誤'HTTP /1.1 400錯誤請求和無響應。當我刪除標題中的空格時,結果爲「是」,沒有錯誤。你能啓用'display_errors'並將'error_reporting'設置爲E_ALL嗎?可能是PHP產生一定的誤差,這將是有益的 – user2890234

+0

'警告:的file_get_contents(HTTP://網站/ API /路由/停止):未能打開流:HTTP請求失敗! HTTP/1.1 404在73行的/home/*****api.php找不到# –