2017-01-11 40 views
1

私はCURLではなくヘッダーを使用してGETリクエストを送信しようとしています。 問題は何ですか?ここでCURLなしでGETリクエストを送信

は私のかくかくしかじかかくかくしかじかBLAコードです:

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-post-request-without-curl – JavaBoy

+0

@JavaBoy 'file_get_contents'が動作しません。 –

答えて

0

あなたが "承認" した後、ヘッダーのスペースを持っている、これを試してください1:

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

とcURLで' Authorization:Bearer'がうまく動作します –

+0

ローカルサーバでテストしたところ、 'HTTP /1.1 400 Bad Request'と 'no'レスポンス。ヘッダー内のスペースを削除したところ、結果は「はい」で、エラーはありませんでした。 'display_errors'を有効にして、' error_reporting'をE_ALLに設定できますか? PHPが役に立つかもしれない何らかのエラーを生成するかもしれない – user2890234

+0

'警告:file_get_contents(http:// site/api/route/stops):ストリームのオープンに失敗しました:HTTPリクエストは失敗しました! HTTP/1.1 404が/home/*****api.phpの73行目に見つかりませんでした。 ' –

関連する問題