我试图将此命令行解释为curl php
curl命令:
curl https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "{clientId}:{secret}" \
-d "grant_type=client_credentials"
这是我的curl php代码:
$url = 'https://api.sandbox.paypal.com/v1/oauth2/token';
$info = array(
'grant_type' =>'client_credentials'
);
$post_field_string = http_build_query($info, '', '&');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept-Language: en_US')
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD,'AQHeHhDPIpAoxWaNkXOwCNFV4cZUobhqDa_4qHPKh0iDrSd6rLaxIknT-lmgwd:EJHLmhBMT9dB48kou4V0jyJzaq-CqUlY0zS6QKsxOZKI15hZHZjTfoSV7MO8we');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_field_string );
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_HEADER,1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
$exec = curl_exec($ch);
curl_exec($ch);
curl_close($ch);
我得到了这个结果:
Resource id #2
我刚刚卷曲,但我正在尝试研究它。这是我的curl php中的正确代码吗?
我希望这个样本响应来自curl:
{
"scope": "https://api.paypal.com/v1/payments/.* https://api.paypal.com/v1/vault/credit-card https://api.paypal.com/v1/vault/credit-card/.*",
"access_token": "EEwJ6tF9x5WCIZDYzyZGaz6Khbw7raYRIBV_WxVvgmsG",
"token_type": "Bearer",
"app_id": "APP-6XR95014BA15863X",
"expires_in": 28800
}
答案 0 :(得分:11)
试试这段代码:
如果要传递数组或字符串,则不需要 CURLOPT_HTTPHEADER
。
已添加
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
绕过SSL
<?php
$url = 'https://api.sandbox.paypal.com/v1/oauth2/token';
$info = array(
'grant_type' =>'client_credentials'
);
$post_field_string = http_build_query($info, '', '&');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERPWD,'ATKsMxDPf23rhQTgixcTYxLfuJoBsTiIRyaSQW_4J8_rNoVQsXHQkBjmBN0z:EOvF6RBizzf9qH2eA_s3PYmQk--smR6Xe8kDws228lq5pA0IebXTg902FY7f');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_field_string);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_HEADER,1);
$exec = curl_exec($ch);
echo '<pre>';
print_r($exec);
curl_close($ch);
<强>响应:强>
HTTP / 1.1 200 OK服务器:Apache-Coyote / 1.1
PROXY_SERVER_INFO:host = slcsbjava3.slc.paypal.com; threadId = 234251 Paypal-Debug-Id:976e66d30ed12
SERVER_INFO: identitysecuretokenserv:v1.oauth2.token&安培; CalThreadId = 138269&安培; TopLevelTxnStartTime = 14666662622&安培;主机= slcsbidensectoken502.slc.paypal.com&安培; PID = 17346
CORRELATION-ID:976e66d30ed12
日期:2014年6月4日星期三,格林尼治标准时间10:21:51
Content-Type:application / json
Transfer-Encoding:chunked
{
"scope": "openid",
"access_token": "A015wXWyeWOj3CprA4dz8uvB.AgGUE-A-p6SuQhw..rmGug",
"token_type": "Bearer",
"expires_in": 28800
}