我已经检查了这个职位,但不是为我工作:API request works with cURL but not with Guzzle?
访问令牌的PHP脚本非常适合我
初始化
$_consumerKey = "consumer key";
$method = "POST";
$endpoint = 'https://api.twitter.com/oauth/request_token';
$callbackUrl = "https://localhost/script/test.php";
$authorizationParams = array(
'oauth_callback' => $callbackUrl,
'oauth_consumer_key' => $_consumerKey,
'oauth_nonce' => md5( microtime() . mt_rand() ),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
);
准备签名
function getSignature( $method, $endpoint, $authorizationParams, $urlParams = array() ) {
$authorizationParams = array_merge( $authorizationParams, $urlParams );
uksort( $authorizationParams, 'strcmp' );
foreach ( $authorizationParams as $key => $value ) {
$authorizationParams[$key] = rawurlencode( $key ) . '=' . rawurlencode( $value );
}
$signatureBase = array(
rawurlencode( $method ),
rawurlencode( $endpoint ),
rawurlencode( implode( '&', $authorizationParams ) ),
);
$signatureBaseString = implode( '&', $signatureBase );
$signatureKey = array(
rawurlencode("consumer token"),
""
);
$signatureKeyString = implode('&', $signatureKey);
return base64_encode(hash_hmac('sha1', $signatureBaseString, $signatureKeyString, true));
}
将数组转换为字符串
function getAuthorizationString( $authorizationParams ) {
$authorizationString = 'Authorization: OAuth';
$count = 0;
foreach ( $authorizationParams as $key => $value ) { // loop over authorization params array
$authorizationString .= !$count ? ' ' : ', ';
$authorizationString .= rawurlencode( $key ) . '="' . rawurlencode( $value ) . '"';
$count++;
}
return $authorizationString;
}
$authorizationParams['oauth_signature'] = getSignature( $method, $endpoint, $authorizationParams );
$apiParams = array(
'method' => $method,
'endpoint' => $endpoint,
'authorization' => getAuthorizationString( $authorizationParams )
);
** curl 请求**
$curlOptions = array(
CURLOPT_URL => $apiParams['endpoint'],
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
$apiParams['authorization'],
'Expect:'
)
);
$curlOptions[CURLOPT_POST] = TRUE;
$ch = curl_init();
curl_setopt_array( $ch, $curlOptions );
$apiResponse = curl_exec( $ch );
print_r($apiResponse);
现在,当我尝试用Guzzle Http替换curl时,我得到以下错误。注意错误详细信息,它说回调URL与应用程序中注册的URL不相同。但我使用的回调URL与CURL中使用的相同
致命错误:未捕获的GuzzleHttp\异常\客户端异常:客户端错误:POST https://api.twitter.com/oauth/request_token
导致403 Forbidden
响应:回调URL不允许用于此客户端应用程序。A(被截断...)位于C:\xampp\htdocs\script\vendor\guzzlehttp\guzzle\src\Exception\RequestException中。php:113堆栈跟踪:(69):我想知道的是,在这个过程中,你会遇到什么样的问题。(204)您访问的页面不存在,请检查您访问的页面是否存在异常。(153):我是一个很好的中间件,我可以帮你做很多事情,但是我不想让你失望。(48):我是一个很棒的人,我的朋友,我的朋友,我的朋友,我的朋友,我的朋友,我的朋友,我的朋友,我的朋友,我的朋友,我的朋友,我的朋友,我的朋友,我的朋友,我的朋友,我的朋友,我的朋友,我的朋友,我的朋友。在第113行的C:中的GuzzleHttp\承诺\承诺::GuzzleHttp
** curl 代码对我不起作用**
初始化
include "vendor/autoload.php";
$client = new \GuzzleHttp\Client();
$_consumerKey = "consumer id";
$method = "POST";
$endpoint = 'https://api.twitter.com/oauth/request_token';
$callbackUrl = "https://localhost/test.php";
$authorizationParams = array(
'oauth_callback' => $callbackUrl,
'oauth_consumer_key' => $_consumerKey,
'oauth_nonce' => md5( microtime() . mt_rand() ),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
);
签名创建
function getSignature( $method, $endpoint, $authorizationParams, $urlParams = array() ) {
$authorizationParams = array_merge( $authorizationParams, $urlParams );
uksort( $authorizationParams, 'strcmp' );
foreach ( $authorizationParams as $key => $value ) {
$authorizationParams[$key] = rawurlencode( $key ) . '=' . rawurlencode( $value );
}
$signatureBase = array(
rawurlencode( $method ),
rawurlencode( $endpoint ),
rawurlencode( implode( '&', $authorizationParams ) ),
);
$signatureBaseString = implode( '&', $signatureBase );
$signatureKey = array(
rawurlencode("consumer secret"),
""
);
$signatureKeyString = implode('&', $signatureKey);
return base64_encode(hash_hmac('sha1', $signatureBaseString, $signatureKeyString, true));
}
将数组转换为字符串
function getAuthorizationString( $authorizationParams ) {
$authorizationString = '';
$count = 0;
foreach ( $authorizationParams as $key => $value ) { // loop over authorization params array
$authorizationString .= !$count ? ' ' : ', ';
$authorizationString .= rawurlencode( $key ) . '="' . rawurlencode( $value ) . '"';
$count++;
}
return $authorizationString;
}
$authorizationParams['oauth_signature'] = getSignature( $method, $endpoint, $authorizationParams );
$apiParams = array(
'method' => $method,
'endpoint' => $endpoint,
'authorization' => getAuthorizationString( $authorizationParams ),
'url_params' => array()
);
Guzzle Http请求
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $apiParams['endpoint'], [
'headers' => [
'Content-Type' => 'application/json',
"Authorization" => "OAuth " . $apiParams['authorization']
]
]);
$result = json_decode($response->getBody()->getContents(), true);
echo "<pre>";
print_r($result);
1条答案
按热度按时间7nbnzgx91#
此函数的返回值为
Authorization: OAuth oauth_callback="https://localhost/script/test.php", oauth_consumer_key="consumer key", ....
然后将其赋值给
$apiParams['authorization']
最后,它连接到头,现在您得到
Authorization => OAuth Authorization: OAuth oauth_callback="https://localhost/script/test.php", oauth_consumer_key="consumer key", ....
,这显然是无效的。总之,需要用空字符串初始化
$authorizationString
。