2018-12-07 09:51:55 257次浏览 0条回答 0 悬赏 10 金钱

我用命令行curl请求有数据返回,但是我用guzzlehttp写代码请求就返回403,请大神指教。 curl.pngpage_guzzle.png

curl命令行代码: curl -H 'host: istore.oppomobile.com' -H 'locale: zh-CN;cn' -H 'appversion: 6.0.0' -H 'appid: HUAWEI#001#cn' -H 'User-Agent: HUAWEI%2FHUAWEI+GRA-CL10%2F19%2F4.4.2%2F0%2F2%2F2101%2F6002' -H 'pid: 001' -H 'sg: 54cd1dd2f970e0d37a63b44e4cdbedcf36b3c402' -H 'ct: 1544089120405' -H 'nw: 1' -H 'ocs: HUAWEI%2FHUAWEI+GRA-CL10%2F19%2F4.4.2%2F0%2F2%2Fsamsung-user+4.4.2+KOT49H+3.8.017.0602+release-keys%2F6002' -H 'country: cn' -H 'sign: 2cfcf190f13f2c0c049b46eb49faad2c' -H 'id: 864394010242461' -H 't: 1544089120400' -H 'oak: cdb09c43063ea6bb' -H 'rsq: 19961' -H 'ch: 2101' -H 'Accept: application/x-protostuff; charset=UTF-8' --compressed 'https://istore.oppomobile.com/card/store/v3/rank/game?installRemoval=1&size=10&start=0'

guzzlehttp代码:

$data   = [
            'installRemoval' => 1,
            'size'           => 1,
            'start'          => 0
        ];
        $headers = [
            'host'       => 'istore.oppomobile.com',
            'locale'     => 'zh-CN;cn',
            'appversion' => '6.0.0',
            'appid'      => 'HUAWEI#001#cn',
            'User-Agent' => 'HUAWEI%2FHUAWEI+GRA-CL10%2F19%2F4.4.2%2F0%2F2%2F2101%2F6002',
            'pid'        => '001',
            'sg'         => '54cd1dd2f970e0d37a63b44e4cdbedcf36b3c402',
            'ct'         => '1544089120405',
            'nw'         => '1',
            'ocs'        => 'HUAWEI%2FHUAWEI+GRA-CL10%2F19%2F4.4.2%2F0%2F2%2Fsamsung-user+4.4.2+KOT49H+3.8.017.0602+release-keys%2F6002',
            'country'    => 'cn',
            'sign'       => '2cfcf190f13f2c0c049b46eb49faad2c',
            'id'         => '864394010242461',
            't'          => '1544089120400',
            'oak'        => 'cdb09c43063ea6bb',
            'rsq'        => '19961',
            'ch'         => '2101',
            'Accept'     => 'application/x-protostuff; charset=UTF-8',
            'Accept-Encoding' => 'deflate,gzip'
        ];

        $client = new Client([
            'timeout' => 5.0,
        ]);
        $response = $client->request('GET', 'https://istore.oppomobile.com/card/store/v3/rank/game', [
            'query' => $data,
            'headers' => $headers,
            'debug' => true
        ]);
        $r        = $response->getBody()->getContents();
        var_dump($r);```
        




补充于 2018-12-07 09:58

补上直接curl的代码:

protected function curlRequests($url = '', $data = '', $request_type = 'get', $header = '')
{
    $ch = curl_init();
    $timeout = 10;
    if ($request_type == 'get') {
        if (!empty($data)) {
            $url .= '?' . http_build_query($data);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
    } else {
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        if (!empty($data)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        }
    }
    if (!empty($header)) {
        $curl_header = [];
        foreach ($header as $key => $v) {
            $curl_header[] = $key . ':' . $v;
        }
        curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_header);
    }
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);//发起连接前等待
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);//接收数据时超时设置

    $handles      = curl_exec($ch);
    $responseInfo = curl_getinfo($ch);
    var_dump($responseInfo);die;
    if ($responseInfo['http_code'] != 200) {
        $error = curl_error($ch);
        return false;
    }
    curl_close($ch);
    if ($handles) {
        return $handles;
    } else {
        return false;
    }
}
亿速云
    没有找到数据。
您需要登录后才可以回答。登录 | 立即注册
蓝灵焰
总监

蓝灵焰

注册时间:2015-10-06
最后登录:2018-12-07
在线时长:30小时0分
粉丝7
金钱4005
威望0
积分4305

热门问题