gpt4 book ai didi

php - Paypal - Zend_Http_Client_Adapter_Curl - 如何打开 CURLOPT_VERBOSE

转载 作者:太空宇宙 更新时间:2023-11-03 16:33:07 25 4
gpt4 key购买 nike

我目前正在开发一个瘦 API 包装器来简化调用代码的实现,以便利用 Paypal 的保险库功能,您可以在其中存储信用卡信息,而不必将其存储在我们的本地系统中。作为 api 包装器的一部分,我正在使用 Zend_Http_Client_Adapter_Curl 并一直遇到“401 未授权”错误,但不确定原因。

为了帮助调试,我试图通过 Zend_Http_Client_Adapter_Curl 选项打开冗长但无济于事。如果我在没有 Zend 层的情况下使用 vanialla php 做同样的事情,我会得到预期的冗长。

有谁知道如何为 Zend_Http_Client_Adapter_Curl 打开详细信息,或者为什么我会收到 401 未经授权的响应?

我的 API 包装器:

public function createVaultItem(Hobis_Api_Payment_Gateway_Item_Vault $item)
{
$client = $this->getHttpClient();

// Authorization:Bearer is not a standard http header, so we have to disable strict header checking
$client->setConfig(array('strict' => false));

//$client->setUri(self::URI_VAULT);

$client->setUri('https://api.sandbox.paypal.com/v1/vault/credit-card');

/**
$payload = array(
'expire_month' => $item->expireMonth,
'expire_year' => $item->expireYear,
'first_name' => $item->nameFirst,
'last_name' => $item->nameLast,
'number' => $item->number,
'payer_id' => $item->userId,
'type' => $item->type
);
*
*/

$payload = array(
"expire_month" => 11,
"expire_year" => 2018,
"first_name" => "Tipsy",
"last_name" => "McStagger",
"number" => 4816123412343720,
"payer_id" => 1,
"type" => "visa"
);

$client->getAdapter()->setConfig(array(
"curloptions" => array(
CURLOPT_VERBOSE => 1,
CURLOPT_FOLLOWLOCATION => 1
)
));

$client->setRawData(json_encode($payload), Hobis_Api_Http_Client::ENC_URLENCODED);

$client->setHeaders(
array(
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization:Bearer' => $this->getAccessToken()
)
);

$response = $client->request(Hobis_Api_Http_Client::POST);

var_dump($client->getLastRequest());

var_dump($client->getLastResponse());
}

响应:

string(351) "POST /v1/vault/credit-card HTTP/1.1
Host: api.sandbox.paypal.com
Accept: application/json
Content-Type: application/json
Authorization:Bearer: 1234
Content-Length: 136

{"expire_month":11,"expire_year":2018,"first_name":"Tipsy","last_name":"McStagger","number":4816123412343720,"payer_id":1,"type":"visa"}"

class Zend_Http_Response#15 (5) {
protected $version => string(3) "1.1"
protected $code => int(401)
protected $message => string(12) "Unauthorized"
protected $headers =>
array(9) {
'Server' => string(17) "Apache-Coyote/1.1"
'Proxy_server_info' => string(43) "host=slcsbjava1.slc.paypal.com;threadId=181"
'Paypal-debug-id' => string(13) "c7eb668ff9035"
'Content-type' => string(16) "application/json"
'Content-length' => string(1) "0"
'Dc' => string(30) "origin2-api.sandbox.paypal.com"
'Date' => string(29) "Wed, 27 Nov 2013 00:57:33 GMT"
'Connection' => string(5) "close"
'Set-cookie' => string(41) "DC=origin2-api.sandbox.paypal.com; secure"
}
protected $body => string(0) ""
}

还有原始代码:

$ch = curl_init('https://api.sandbox.paypal.com/v1/vault/credit-card');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
sprintf('Authorization:Bearer 1234')
)
);

$payload = array(
"expire_month" => 11,
"expire_year" => 2018,
"first_name" => "Tipsy",
"last_name" => "McStagger",
"number" => 4816123412343720,
"payer_id" => 1,
"type" => "visa"
);

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

$response = json_decode(curl_exec($ch));

curl_close($ch);

var_dump(sprintf('State: %s | Mask: %s', $response->state, $response->number));

并输出:

* About to connect() to api.sandbox.paypal.com port 443 (#0)
* Trying 23.5.251.39... * connected
* Connected to api.sandbox.paypal.com (23.5.251.39) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* CAfile: /etc/pki/tls/certs/ca-bundle.crt
CApath: none
* NSS: client certificate not found (nickname not specified)
* SSL connection using SSL_RSA_WITH_RC4_128_SHA
* Server certificate:
* subject: CN=api.sandbox.paypal.com,OU=Partner Support,O="PayPal, Inc.",L=San Jose,ST=CALIFORNIA,C=US
* start date: Aug 20 00:00:00 2013 GMT
* expire date: Aug 21 23:59:59 2015 GMT
* common name: api.sandbox.paypal.com
* issuer: CN=VeriSign Class 3 Secure Server CA - G3,OU=Terms of use at https://www.verisign.com/rpa (c)10,OU=VeriSign Trust Network,O="VeriSign, Inc.",C=US
> POST /v1/vault/credit-card HTTP/1.1
Host: api.sandbox.paypal.com
Accept: */*
Content-Type: application/json
Authorization:Bearer 1234
Content-Length: 136

< HTTP/1.1 201 Created
< Server: Apache-Coyote/1.1
< PROXY_SERVER_INFO: host=slcsbjava3.slc.paypal.com;threadId=444
< Paypal-Debug-Id: 362182266fe6c
< Content-Language: *
< CORRELATION-ID: 362182266fe6c
< SERVER_INFO: vaultplatformserv:v1.vault.credit-card&CalThreadId=89&TopLevelTxnStartTime=1429716e360&Host=slcsbvaultplatformserv502.slc.paypal.com&pid=1239
< Content-Type: application/json
< Content-Length: 686
< DC: origin2-api.sandbox.paypal.com
< Date: Wed, 27 Nov 2013 01:05:29 GMT
< Connection: keep-alive
< Set-Cookie: DC=origin2-api.sandbox.paypal.com; secure
<
* Connection #0 to host api.sandbox.paypal.com left intact
* Closing connection #0

string(34) "State: ok | Mask: xxxxxxxxxxxx3720"

同样,我不确定为什么我会收到 401 Unauthorized,尽管我可能会尝试,但无法获得第一个示例(瘦 API 包装器)来显示详细输出。

最佳答案

乍一看,问题似乎出在您的授权 header 上。我想你想要:

$client->setHeaders(
array(
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Bearer '.$this->getAccessToken()
)
);

为了匹配您的 vanilla PHP 请求。

关于php - Paypal - Zend_Http_Client_Adapter_Curl - 如何打开 CURLOPT_VERBOSE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20231155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com