PHP Guzzle请求无法正常工作。适用于CURL

时间:2015-10-08 11:24:22

标签: php api curl guzzle

所以我刚刚开始尝试理解Guzzle,但我的一个请求仍然会返回错误,即使使用CURL完成相同的请求也能正常工作。他们看起来和我一模一样,但也许我错过了关于Guzzle如何运作的一些东西。出于安全原因,我已经删除了我的访问令牌和交易ID,显然我使用的真实域名已经正确使用了实际的字符串。我使用的是Freeagent API,虽然我不认为我正在谈论的API与它有什么关系。

我确定这是一个简单的错误,源于我不理解Guzzle是如何工作的,但对我而言,这个Guzzle请求和Curl请求看起来完全相同,所以我不明白我为什么会这样做。 m与一个错误和另一个成功请求。

导致错误的Guzzle请求:

$access_token = '{my_access_token_removed}';
$url = 'https://api.freeagent.com/v2/bank_transactions/{myid}';

$guzzleClient = new Client([
    'verify'    =>  false
]);

$request = $guzzleClient->get($url, array(
    'debug'     =>  true,
    'headers'   =>  array(
        'User-Agent' => 'sortitoutsi',
        'Accept' => 'application/json',
        'Content-Type' => 'application/json',
        'Authorization' => $access_token
    )
));

此CURL请求正常工作:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer '.$access_token,
    'Accept: application/json',
    'Content-Type: application/json',
));
curl_setopt($ch, CURLOPT_USERAGENT, 'sortitoutsi');
curl_setopt($ch, CURLOPT_TIMEOUT, 3);

$json_response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

有什么想法吗?我已经复制并粘贴了下面的整个调试信息和堆栈跟踪。我也在使用Laravel,虽然我再也看不出这是一个问题。

  

Middleware.php第69行中的ClientException:客户端错误:400

in Middleware.php line 69
at Middleware::GuzzleHttp\{closure}(object(Response)) in Promise.php line 199
at Promise::callHandler('1', object(Response), array(object(Promise), object(Closure), null)) in Promise.php line 152
at Promise::GuzzleHttp\Promise\{closure}() in TaskQueue.php line 60
at TaskQueue->run() in CurlMultiHandler.php line 96
at CurlMultiHandler->tick() in CurlMultiHandler.php line 123
at CurlMultiHandler->execute(true) in Promise.php line 240
at Promise->invokeWaitFn() in Promise.php line 217
at Promise->waitIfPending() in Promise.php line 261
at Promise->invokeWaitList() in Promise.php line 219
at Promise->waitIfPending() in Promise.php line 62
at Promise->wait() in Client.php line 129
at Client->request('get', 'https://api.freeagent.com/v2/bank_transactions/{id_removed}', array('debug' => true, 'headers' => array('User-Agent' => 'sortitoutsi', 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'Authorization' => '{access_token_removed}'))) in Client.php line 87
at Client->__call('get', array('https://api.freeagent.com/v2/bank_transactions/{id_removed}', array('debug' => true, 'headers' => array('User-Agent' => 'sortitoutsi', 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'Authorization' => '{access_token_removed}')))) in Freeagent.php line 185
at Client->get('https://api.freeagent.com/v2/bank_transactions/{id_removed}', array('debug' => true, 'headers' => array('User-Agent' => 'sortitoutsi', 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'Authorization' => '{access_token_removed}'))) in Freeagent.php line 185
at Freeagent->getTransaction('{id_removed}') in Facade.php line 210
at Facade::__callStatic('getTransaction', array('{id_removed}')) in TestingController.php line 17
at FreeagentApi::getTransaction('{id_removed}') in TestingController.php line 17
at TestingController->getTesting()
at call_user_func_array(array(object(TestingController), 'getTesting'), array()) in Controller.php line 256
at Controller->callAction('getTesting', array()) in ControllerDispatcher.php line 164
at ControllerDispatcher->call(object(TestingController), object(Route), 'getTesting') in ControllerDispatcher.php line 112
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 114
at ControllerDispatcher->callWithinStack(object(TestingController), object(Route), object(Request), 'getTesting') in ControllerDispatcher.php line 69
at ControllerDispatcher->dispatch(object(Route), object(Request), 'Susie\Http\Controllers\TestingController', 'getTesting') in Route.php line 201
at Route->runWithCustomDispatcher(object(Request)) in Route.php line 134
at Route->run(object(Request)) in Router.php line 704
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 706
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 671
at Router->dispatchToRoute(object(Request)) in Router.php line 631
at Router->dispatch(object(Request)) in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Debugbar.php line 49
at Debugbar->handle(object(Request), object(Closure))
at call_user_func_array(array(object(Debugbar), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ActivatedUser.php line 50
at ActivatedUser->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ActivatedUser), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 21
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 122
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
at Kernel->handle(object(Request)) in index.php line 53

这是在浏览器中打印出来的Guzzle调试。

* Hostname was found in DNS cache
* Hostname in DNS cache was stale, zapped
*   Trying 213.129.77.178...
* Connected to api.freeagent.com (213.129.77.178) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* Server certificate:
*    subject: C=GB; ST=City of Edinburgh; L=Edinburgh; O=FreeAgent Central Ltd.; CN=a.ssl.freeagent.com
*    start date: 2014-11-17 00:00:00 GMT
*    expire date: 2017-11-21 12:00:00 GMT
*    issuer: C=US; O=DigiCert Inc; CN=DigiCert SHA2 Secure Server CA
*    SSL certificate verify ok.
> GET /v2/bank_transactions/{id_removed} HTTP/1.1
Host: api.freeagent.com
User-Agent: sortitoutsi
Accept: application/json
Content-Type: application/json
Authorization: {access_token_removed}

< HTTP/1.1 400 Bad Request
* Server nginx is not blacklisted
< Server: nginx
< Date: Thu, 08 Oct 2015 11:14:49 GMT
< Content-Type: application/json; charset=utf-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Status: 400 Bad Request
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< WWW-Authenticate: Bearer
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: DELETE, GET, HEAD, OPTIONS, POST, PUT
< Access-Control-Allow-Headers: Accept, Authorization, Content-Type
< Access-Control-Expose-Headers: Link
< Access-Control-Max-Age: 1728000
< Cache-Control: no-cache
< X-Request-Id: b852b734-2512-4acf-9ae0-474ad1428536
< X-Rev: 6937e9
< X-Host: web3-ash
< X-Runtime: 0.018724
< Set-Cookie: uid=1YFNslYWUCl/7GUlHLMKAg==; expires=Fri, 07-Oct-16 11:14:49 GMT; domain=.freeagent.com; path=/
< 
* Connection #0 to host api.freeagent.com left intact

1 个答案:

答案 0 :(得分:1)

所以我自己想出来了。这只是一个人为错字,应该是:

  

'授权:持票人'。$ access_token

而不是

  

'授权:'。$ access_token

freeagent api实际上是在回复中告诉我的,但是guzzle没有给我错误。

为了得到答案我只是做了:

try {
    $request = $this->guzzle->request('get', $url, array(
        'debug'     =>  true,
        'headers'   =>  array(
            'User-Agent' => 'sortitoutsi',
            'Accept' => 'application/json',
            'Content-Type' => 'application/json',
            'Authorization' => 'Bearer '.$access_token
        )
    ));
} catch (ClientException $e) {
    die((string)$e->getResponse()->getBody());
}

然后我可以根据api

的json响应轻松看到我的错误