php 为什么我得到403禁止与狂饮?

rjee0c15  于 2023-02-18  发布在  PHP
关注(0)|答案(2)|浏览(138)

如果我用curl做响应,API就能工作:

$curl = curl_init();

curl_setopt_array($curl, [
    CURLOPT_URL => config('healy.loginServerHost') . '/token',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => 'grant_type=refresh_token&refresh_token=mytoken&app_version=1.0',
    CURLOPT_USERPWD => config('healy.loginServerUsername') . ':' . config('healy.loginServerPassword'),
]);

$response = curl_exec($curl);

我得到了一个200的响应。但是,当我尝试在GuzzleClient 6中使用相同的响应时,我得到了403禁止的响应。

$client = new GuzzleHttp\Client();

$response = $client->post(config('healy.loginServerHost') . '/token', [
    'form_params' => [
        'grand_type' => 'refresh_token',
        'refresh_token' => 'mytoken',
        'app_version' => 1.0,
    ],
    'http_errors' => false,
    'auth' => [
        config('healy.loginServerUsername'),
        config('healy.loginServerPassword'),
    ],
    'options' => [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    ],
]);

我在狂饮中错过了什么?这些电话不一样吗?

eoigrqb6

eoigrqb61#

没有options request_option,我们在使用类GuzzleHttp\Handler\CurlMultiHandler时使用异步请求的选项。使用curl request_option代替。

$client = new Client([
    'curl' => [
            \CURLOPT_ENCODING => '',
            \CURLOPT_RETURNTRANSFER => true,
            \CURLOPT_MAXREDIRS => 10,
            \CURLOPT_TIMEOUT => 0,
            \CURLOPT_FOLLOWLOCATION => true,
            \CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        ]
    ]);
    
    $response = $client->post(config('healy.loginServerHost') . '/token', [
        'form_params' => [
            'grand_type' => 'refresh_token',
            'refresh_token' => 'mytoken',
            'app_version' => 1.0,
        ],
        'http_errors' => false,
        'auth' => [
            config('healy.loginServerUsername'),
            config('healy.loginServerPassword'),
        ]
    ]);

您也可以在请求中使用curl请求选项,作为选项数组的第三个参数。
但是我建议如果你使用的是Guzzle,那么你应该完全使用它。所以,你可以用Guzzle提供的很多请求选项来替换curl选项。

$client = new GuzzleHttp\Client(['curl' => [\CURLOPT_ENCODING => '', CURLOPT_RETURNTRANSFER => true]]);

$response = $client->post(config('healy.loginServerHost') . '/token', [
    'form_params' => [
        'grand_type' => 'refresh_token',
        'refresh_token' => 'mytoken',
        'app_version' => 1.0,
    ],
    'http_errors' => false,
    'auth' => [
        config('healy.loginServerUsername'),
        config('healy.loginServerPassword'),
    ],
    'allow_redirects' => [
        'max'             => 10,        // allow at most 10 redirects.
        'strict'          => true,      // use "strict" RFC compliant redirects.
        // 'referer'         => true,      // add a Referer header
        'protocols'       => ['https'], // only allow https URLs
        // 'on_redirect'     => $onRedirect,
        'track_redirects' => true
    ],
    'version' => 1.1 // equivalent to CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    // No need of \CURLOPT_TIMEOUT => 0, as it is by default 0, if want any other value then use timeout request options
]);
fafcakar

fafcakar2#

对于那些谁仍然挣扎与此可以添加用户代理头-

$client = new GuzzleHttp\Client([
        'headers' => [
            'User-Agent' => $_SERVER['HTTP_USER_AGENT'],
        ]
    ]);

// Request code will continue here

希望这能解决问题

相关问题