使用Guzzle获取远程数据

时间:2017-02-28 15:39:56

标签: php http guzzlehttp

我想使用Guzzle从网页上获取数据。我写了以下代码:

<?php

require_once 'vendor/autoload.php';

$client = new \GuzzleHttp\Client(['base_uri' => 'https://yandex.ru']);

$response = $client->request('GET');

$body = $response->getBody();

echo $body;

但是当我在本地主机上运行此代码时收到致命错误。

Fatal error: Uncaught exception 'GuzzleHttp\Exception\RequestException' with message 'cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)' in C:\Xampp\htdocs\Guzzle\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php:187 Stack trace: #0 C:\Xampp\htdocs\Guzzle\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(150): GuzzleHttp\Handler\CurlFactory::createRejection(Object(GuzzleHttp\Handler\EasyHandle), Array) #1 C:\Xampp\htdocs\Guzzle\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(103): GuzzleHttp\Handler\CurlFactory::finishError(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory)) #2 C:\Xampp\htdocs\Guzzle\vendor\guzzlehttp\guzzle\src\Handler\CurlHandler.php(43): GuzzleHttp\Handler\CurlFactory::finish(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory)) #3 C:\Xampp\htdocs\Guzzle\vendor\guzzlehttp\guzzle in C:\Xampp\htdocs\Guzzle\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php on line 187

问题在于,当我删除代码的最后三行时,它运行良好,因此问题在于请求。有人可以提出解决方案吗?

1 个答案:

答案 0 :(得分:1)

  

当curl.cainfo和openssl.cafile配置时发生此错误   php.ini文件的属性,不要以任何有效证书为目标   允许您使用 ssl 创建连接,因为它们无效。

使用https://yandex.ru通过ssl连接,但不是httpbin.org情况。这就是您没有获得SSL证书问题的原因。

作为解决方案,我建议您article