来自 curl php 的资源 id #2 响应
Posted
技术标签:
【中文标题】来自 curl php 的资源 id #2 响应【英文标题】:Resource id #2 response from curl php 【发布时间】:2014-07-24 19:25:08 【问题描述】:我正在尝试将此命令行解释为 curl php
卷曲命令:
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 的新手,但我正在努力学习它。那是我的 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
【问题讨论】:
【参考方案1】:试试这个代码:
如果您传递数组或字符串,则不需要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
服务器信息: identitysecuretokenserv:v1.oauth2.token&CalThreadId=138269&TopLevelTxnStartTime=14666662622&Host=slcsbidensectoken502.slc.paypal.com&pid=17346
相关 ID:976e66d30ed12
日期:格林威治标准时间 2014 年 6 月 4 日星期三 10:21:51
内容类型:application/json
传输编码:分块
"scope": "openid",
"access_token": "A015wXWyeWOj3CprA4dz8uvB.AgGUE-A-p6SuQhw..rmGug",
"token_type": "Bearer",
"expires_in": 28800
【讨论】:
注意! 禁用CURLOPT_SSL_VERIFYPEER
和CURLOPT_SSL_VERIFYHOST
不是安全选项;它使 MITM 攻击更容易长期存在。以上是关于来自 curl php 的资源 id #2 响应的主要内容,如果未能解决你的问题,请参考以下文章
Curl / php 来自 PayPal IPN url 的空响应
PHP / curl:namelookup_time / dns减慢请求