php – 将Curl客户端ssl移动到Guzzle

前端之家收集整理的这篇文章主要介绍了php – 将Curl客户端ssl移动到Guzzle前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用Guzzle v3.9.2,PHP 5.3和PHP 5.5.

我有以下工作卷曲代码,使用ssl客户端证书:

$url = "https://example.com/";
$cert_file = '/path/to/certificate.pem';

$ch = curl_init();
$options = array(
  CURLOPT_RETURNTRANSFER => true,CURLOPT_FOLLOWLOCATION => true,CURLOPT_URL => $url,CURLOPT_SSLCERT => $cert_file,);

curl_setopt_array($ch,$options);
$output = curl_exec($ch);

if (!$output) {
  echo "Curl Error : " . curl_error($ch);
}
else {
  echo htmlentities($output);
}

我试图把它移动到Guzzle:

require '/var/www/vendor/autoload.PHP';
use Guzzle\Http\Client;
$client = new Client();
$request = $client->get($url,array('cert' => $cert_file));
$response = $client->send($request);
echo $response . PHP_EOL;
print 'HI' . PHP_EOL;

当我使用curl运行它,我得到200响应.当我使用Guzzle我得到一个403.

尝试这样:
$client = new Client();
 $response = $client->get($url,array(),array('cert' => $cert_file));

并检查添加以下行:

$this->assertEquals($cert_file,$request->getCurlOptions()->get(CURLOPT_SSLCERT));

或使用这个:

$client = new Client();
 $request = $client->createRequest('GET',$url);
 $request->getCurlOptions()->set(CURLOPT_SSLCERT,$cert_file);
 $response = $client->send($request);

如果您使用自己的唱片证书设置此选项:

$request->getCurlOptions()->set(CURLOPT_SSL_VERIFYHOST,false);
$request->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER,false);

发送请求之前设置此行:

$request = $client->get( .... )
.
.
.
$request->setResponse(new Response(200),true);
$request->send();

检查你的网址并输入它如下所示:

$url = 'https://example.com/index.PHP';

您可以添加默认选项,如您的卷曲代码

$request->getCurlOptions()->set(CURLOPT_RETURNTRANSFER,true);
$request->getCurlOptions()->set(CURLOPT_FOLLOWLOCATION,true);

猜你在找的PHP相关文章