将cURL命令行转换为PHP cURL [英] Translate cURL command line into PHP cURL

查看:239
本文介绍了将cURL命令行转换为PHP cURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将此cURL命令翻译成PHP cURL代码:

I need to translate this cURL command into PHP cURL code:


> curl --get 'https://api.twitter.com/1/followers/ids.json' --data
> 'cursor=-1&screen_name=somename' --header 'Authorization: OAuth
> oauth_consumer_key="key", oauth_nonce="nonce",
> oauth_signature="signature", oauth_signature_method="HMAC-SHA1",
> oauth_timestamp="timestamp", oauth_token="token", oauth_version="1.0"'
> --verbose


似乎工作:

> $ch = curl_init();
> curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth oauth_consumer_key="key", oauth_nonce="nonce", oauth_signature="signature", oauth_signature_method="HMAC-SHA1", oauth_timestamp="timestamp", oauth_token="token", oauth_version="1.0"'));
> curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
> curl_setopt($ch, CURLOPT_VERBOSE, 1);
> curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
> curl_setopt($ch, CURLOPT_HTTPGET, 1);
> curl_setopt($ch, CURLOPT_URL, 'https://api.twitter.com/1/users/show.json?cursor=-1&screen_name=somename');
> $page = curl_exec($ch);
> curl_close($ch);

错误我得到了


SSL证书问题,请验证CA证书是否正确。详细信息:错误:14090086:SSL例程:SSL3_GET_SERVER_CERTIFICATE:证书验证失败

SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

但是它适用于标准curl命令

however it works in the standard curl command

推荐答案

您需要为证书链提供curl,以允许它将Twitter的SSL证书验证为有效。为此,请从此处下载必要的证书签名从此处,并将其保存到一个普通文件(我会假设你命名为 cacert.pem )。

You need to provide curl with the certificate chain that will allow it to verify Twitter's SSL certificate as valid. To do this, download the requisite certificate signatures from here and save them into a plain file (I 'll assume you name it cacert.pem).

,将 CURLOPT_CAINFO 设置为指向此文件:

Then, before making the request, set CURLOPT_CAINFO to point to this file:

// assumes file in same directory as script
curl_setopt($ch, CURLOPT_CAINFO, 'cacert.pem');

这也是一个好主意,明确启用SSL证书验证,而不是依赖于默认设置:

It's also a good idea to explicitly enable SSL certificate verification instead of relying on default settings:

curl_setopt($ch, CURLOPT_VERIFYPEER, true);

这篇关于将cURL命令行转换为PHP cURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆