我正在将Guzzle v3.9.2与php 5.3和php 5.5一起使用。
我有以下使用ssl客户端证书的工作curl代码:
$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';
并且您可以添加默认选项,例如您的curl代码:
$request->getCurlOptions()->set(CURLOPT_RETURNTRANSFER , true);
$request->getCurlOptions()->set(CURLOPT_FOLLOWLOCATION , true);
关于php - 将Curl客户端SSL移至Guzzle,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25924147/