发送没有CURL的GET请求

时间:2017-01-11 09:44:21

标签: php http get

我试图在没有CURL的情况下发送GET请求但是使用Header。 有什么问题?

这是我的bla bla bla bla bla bla 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

}

这是$ options返回值:

    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
        )

)

1 个答案:

答案 0 :(得分: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

}