PHP使用Guzzle依赖实现对外部接口封装

发送请求

你可以使用Guzzle的 GuzzleHttp\ClientInterface 对象来发送请求。

创建客户端

use GuzzleHttp\Client;

$client = new Client([
    // Base URI is used with relative requests
    'base_uri' => 'http://httpbin.org',
    // You can set any number of default request options.
    'timeout'  => 2.0,
]);

Client对象可以接收一个包含参数的数组:

base_uri

(string|UriInterface) 基URI用来合并到相关URI,可以是一个字符串或者UriInterface的实例,当提供了相关uri,将合并到基URI,遵循的规则请参考 RFC 3986, section 2 章节。

// Create a client with a base URI
$client = new GuzzleHttp\Client(['base_uri' => 'https://foo.com/api/']);
// Send a request to https://foo.com/api/test
$response = $client->request('GET', 'test');
// Send a request to https://foo.com/root
$response = $client->request('GET', '/root');

不想阅读RFC 3986?这里有一些关于 base_uri 与其他URI处理器的快速例子:

base_uri URI Result
http://foo.com /bar http://foo.com/bar
http://foo.com/foo /bar http://foo.com/bar
http://foo.com/foo bar http://foo.com/bar
http://foo.com/foo/ bar http://foo.com/foo/bar
http://foo.com http://baz.com http://baz.com
http://foo.com/?bar bar http://foo.com/bar
handler
传输HTTP请求的(回调)函数。 该函数被调用的时候包含   Psr7\Http\Message\RequestInterface  以及参数数组,必须返回   GuzzleHttp\Promise\PromiseInterface  ,成功时满足 Psr7\Http\Message\ResponseInterface  。   handler  是一个构造方法,不能在请求参数里被重写。
...
(混合) 构造方法中传入的其他所有参数用来当作每次请求的默认参数。

发送请求

Client对象的方法可以很容易的发送请求:

$response = $client->get('http://httpbin.org/get');
$response = $client->delete('http://httpbin.org/delete');
$response = $client->head('http://httpbin.org/get');
$response = $client->options('http://httpbin.org/get');
$response = $client->patch('http://httpbin.org/patch');
$response = $client->post('http://httpbin.org/post');
$response = $client->put('http://httpbin.org/put');

你可以创建一个请求,一切就绪后将请求传送给client:

use GuzzleHttp\Psr7\Request;

$request = new Request('PUT', 'http://httpbin.org/put');
$response = $client->send($request, ['timeout' => 2]);

Client对象为传输请求提供了非常灵活的处理器方式,包括请求参数、每次请求使用的中间件以及传送多个相关请求的基URI。

你可以在 Handlers and Middleware 页面找到更多关于中间件的内容。

异步请求

你可以使用Client提供的方法来创建异步请求:

$promise = $client->getAsync('http://httpbin.org/get');
$promise = $client->deleteAsync('http://httpbin.org/delete');
$promise = $client->headAsync('http://httpbin.org/get');
$promise = $client->optionsAsync('http://httpbin.org/get');
$promise = $client->patchAsync('http://httpbin.org/patch');
$promise = $client->postAsync('http://httpbin.org/post');
$promise = $client->putAsync('http://httpbin.org/put');

你也可以使用Client的 sendAsync() and requestAsync() 方法:

use GuzzleHttp\Psr7\Request;

// Create a PSR-7 request object to send
$headers = ['X-Foo' => 'Bar'];
$body = 'Hello!';
$request = new Request('HEAD', 'http://httpbin.org/head', $headers, $body);

// Or, if you don't need to pass in a request instance:
$promise = $client->requestAsync('GET', 'http://httpbin.org/get');

这些方法返回了Promise对象,该对象实现了由 Guzzle promises library 提供的 Promises/A+ spec ,这意味着你可以使用 then() 来调用返回值,成功使用 Psr\Http\Message\ResponseInterface 处理器,否则抛出一个异常。

use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;

$promise = $client->requestAsync('GET', 'http://httpbin.org/get');
$promise->then(
    function (ResponseInterface $res) {
        echo $res->getStatusCode() . "\n";
    },
    function (RequestException $e) {
        echo $e->getMessage() . "\n";
        echo $e->getRequest()->getMethod();
    }
);

并发请求

你可以使用Promise和异步请求来同时发送多个请求:

use GuzzleHttp\Client;
use GuzzleHttp\Promise;

$client = new Client(['base_uri' => 'http://httpbin.org/']);

// Initiate each request but do not block
$promises = [
    'image' => $client->getAsync('/image'),
    'png'   => $client->getAsync('/image/png'),
    'jpeg'  => $client->getAsync('/image/jpeg'),
    'webp'  => $client->getAsync('/image/webp')
];

// Wait on all of the requests to complete.
$results = Promise\unwrap($promises);

// You can access each result using the key provided to the unwrap
// function.
echo $results['image']->getHeader('Content-Length');
echo $results['png']->getHeader('Content-Length');

当你想发送不确定数量的请求时,可以使用 GuzzleHttp\Pool 对象:

use GuzzleHttp\Pool;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

$client = new Client();

$requests = function ($total) {
    $uri = 'http://127.0.0.1:8126/guzzle-server/perf';
    for ($i = 0; $i < $total; $i++) {
        yield new Request('GET', $uri);
    }
};

$pool = new Pool($client, $requests(100), [
    'concurrency' => 5,
    'fulfilled' => function ($response, $index) {
        // this is delivered each successful response
    },
    'rejected' => function ($reason, $index) {
        // this is delivered each failed request
    },
]);

// Initiate the transfers and create a promise
$promise = $pool->promise();

// Force the pool of requests to complete.
$promise->wait();

使用响应

前面的例子里,我们取到了 $response 变量,或者从Promise得到了响应,Response对象实现了一个PSR-7接口 Psr\Http\Message\ResponseInterface , 包含了很多有用的信息。

你可以获取这个响应的状态码和和原因短语(reason phrase):

$code = $response->getStatusCode(); // 200
$reason = $response->getReasonPhrase(); // OK

你可以从响应获取头信息(header):

// Check if a header exists.
if ($response->hasHeader('Content-Length')) {
    echo "It exists";
}

// Get a header from the response.
echo $response->getHeader('Content-Length');

// Get all of the response headers.
foreach ($response->getHeaders() as $name => $values) {
    echo $name . ': ' . implode(', ', $values) . "\r\n";
}

使用 getBody 方法可以获取响应的主体部分(body),主体可以当成一个字符串或流对象使用

$body = $response->getBody();
// Implicitly cast the body to a string and echo it
echo $body;
// Explicitly cast the body to a string
$stringBody = (string) $body;
// Read 10 bytes from the body
$tenBytes = $body->read(10);
// Read the remaining contents of the body as a string
$remainingBytes = $body->getContents();

查询字符串参数

你可以有多种方式来提供请求的查询字符串 你可以在请求的URI中设置查询字符串:

$response = $client->request('GET', 'http://httpbin.org?foo=bar');

你可以使用 query 请求参数来声明查询字符串参数:

$client->request('GET', 'http://httpbin.org', [
    'query' => ['foo' => 'bar']
]);

提供的数组参数将会使用PHP的 http_build_query :

最后,你可以提供一个字符串作为 query 请求参数:

$client->request('GET', 'http://httpbin.org', ['query' => 'foo=bar']);

上传数据

Guzzle为上传数据提供了一些方法。 你可以发送一个包含数据流的请求,将 body 请求参数设置成一个字符串、 fopen 返回的资源、或者一个 Psr\Http\Message\StreamInterface 的实例。

// Provide the body as a string.
$r = $client->request('POST', 'http://httpbin.org/post', [
    'body' => 'raw data'
]);

// Provide an fopen resource.
$body = fopen('/path/to/file', 'r');
$r = $client->request('POST', 'http://httpbin.org/post', ['body' => $body]);

// Use the stream_for() function to create a PSR-7 stream.
$body = \GuzzleHttp\Psr7\stream_for('hello!');
$r = $client->request('POST', 'http://httpbin.org/post', ['body' => $body]);

上传JSON数据以及设置合适的头信息可以使用 json 请求参数这个简单的方式:

$r = $client->request('PUT', 'http://httpbin.org/put', [
    'json' => ['foo' => 'bar']
]);

POST/表单请求

除了使用 body 参数来指定请求数据外,Guzzle为发送POST数据提供了有用的方法。

发送表单字段

发送 application/x-www-form-urlencoded POST请求需要你传入 form_params 数组参数,数组内指定POST的字段。

$response = $client->request('POST', 'http://httpbin.org/post', [
    'form_params' => [
        'field_name' => 'abc',
        'other_field' => '123',
        'nested_field' => [
            'nested' => 'hello'
        ]
    ]
]);
发送表单文件

你可以通过使用 multipart 请求参数来发送表单(表单enctype属性需要设置 multipart/form-data )文件, 该参数接收一个包含多个关联数组的数组,每个关联数组包含一下键名:

  • name: (必须,字符串) 映射到表单字段的名称。
  • contents: (必须,混合) 提供一个字符串,可以是 fopen 返回的资源、或者一个

Psr\Http\Message\StreamInterface 的实例。

$response = $client->request('POST', 'http://httpbin.org/post', [
    'multipart' => [
        [
            'name'     => 'field_name',
            'contents' => 'abc'
        ],
        [
            'name'     => 'file_name',
            'contents' => fopen('/path/to/file', 'r')
        ],
        [
            'name'     => 'other_file',
            'contents' => 'hello',
            'filename' => 'filename.txt',
            'headers'  => [
                'X-Foo' => 'this is an extra header to include'
            ]
        ]
    ]
]);

Cookies

Guzzle可以使用 cookies 请求参数为你维护一个cookie会话,当发送一个请求时, cookies 选项必须设置成 GuzzleHttp\Cookie\CookieJarInterface 的实例。

// Use a specific cookie jar
$jar = new \GuzzleHttp\Cookie\CookieJar;
$r = $client->request('GET', 'http://httpbin.org/cookies', [
    'cookies' => $jar
]);

You can set cookies to true in a client constructor if you would like to use a shared cookie jar for all requests.

// Use a shared client cookie jar
$client = new \GuzzleHttp\Client(['cookies' => true]);
$r = $client->request('GET', 'http://httpbin.org/cookies');

重定向

如果你没有告诉Guzzle不要重定向,Guzzle会自动的进行重定向,你可以使用 allow_redirects 请求参数来自定义重定向行为。

  • 设置成 true 时将启用最大数量为5的重定向,这是默认设置。
  • 设置成 false 来禁用重定向。
  • 传入一个包含 max 键名的关联数组来声明最大重定向次数,提供可选的 strict 键名来声明是否使用严格的RFC标准重定向 (表示使用POST请求重定向POST请求 vs 大部分浏览器使用GET请求重定向POST请求)。
$response = $client->request('GET', 'http://github.com');
echo $response->getStatusCode();
// 200

下面的列子表示重定向被禁止:

$response = $client->request('GET', 'http://github.com', [
    'allow_redirects' => false
]);
echo $response->getStatusCode();
// 301

异常

请求传输过程中出现的错误Guzzle将会抛出异常。

  • 在发送网络错误(连接超时、DNS错误等)时,将会抛出 GuzzleHttp\Exception\RequestException 异常。 该异常继承自 GuzzleHttp\Exception\TransferException ,捕获这个异常可以在传输请求过程中抛出异常。

    use GuzzleHttp\Exception\RequestException;
    
    try {
        $client->request('GET', 'https://github.com/_abc_123_404');
    } catch (RequestException $e) {
        echo $e->getRequest();
        if ($e->hasResponse()) {
            echo $e->getResponse();
        }
    }
    
  • GuzzleHttp\Exception\ConnectException 异常发生在网络错误时, 该异常继承自 GuzzleHttp\Exception\RequestException 。

  • 如果 http_errors 请求参数设置成true,在400级别的错误的时候将会抛出 GuzzleHttp\Exception\ClientException 异常, 该异常继承自 GuzzleHttp\Exception\BadResponseException GuzzleHttp\Exception\BadResponseException 继承自GuzzleHttp\Exception\RequestException 。

    use GuzzleHttp\Exception\ClientException;
    
    try {
        $client->request('GET', 'https://github.com/_abc_123_404');
    } catch (ClientException $e) {
        echo $e->getRequest();
        echo $e->getResponse();
    }
    
  • 如果 http_errors 请求参数设置成true,在500级别的错误的时候将会抛出 GuzzleHttp\Exception\ServerException 异常。 该异常继承自 GuzzleHttp\Exception\BadResponseException 。

  • GuzzleHttp\Exception\TooManyRedirectsException 异常发生在重定向次数过多时, 该异常继承自 GuzzleHttp\Exception\RequestException 。

上述所有异常均继承自 GuzzleHttp\Exception\TransferException 。

环境变量

Guzzle提供了一些可自定义的环境变量:

GUZZLE_CURL_SELECT_TIMEOUT
当在curl处理器时使用   curl_multi_select()  控制了 curl_multi_* 需要使用到的持续时间, 有些系统实现PHP的   curl_multi_select()  存在问题,调用该函数时总是等待超时的最大值。
HTTP_PROXY
定义了使用http协议发送请求时使用的代理。
HTTPS_PROXY
定义了使用https协议发送请求时使用的代理。

相关ini设置

Guzzle配置客户端时可以利用PHP的ini配置。

openssl.cafile
当发送到"https"协议的请求时需要用到指定磁盘上PEM格式的CA文件,参考:   https://wiki.php.net/rfc/tls-peer-verification#phpini_defaults
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Laravel 中使用 Goutte 和 Guzzle 实现一个简单的爬虫非常容易,下面是一个简单的示例,步骤如下: 1. 安装 Guzzle 和 Goutte 在 Laravel 项目中使用 Composer 安装 Guzzle 和 Goutte: ``` composer require guzzlehttp/guzzle composer require symfony/dom-crawler ``` 2. 创建一个命令 在 Laravel 中,您可以使用 `make:command` Artisan 命令创建一个命令来运行您的爬虫,例如: ``` php artisan make:command Spider ``` 这将在 `app/Console/Commands` 目录中创建一个名为 `Spider` 的新命令。 3. 编写命令代码 在 `app/Console/Commands/Spider.php` 文件中,您可以编写以下代码: ```php <?php namespace App\Console\Commands; use Illuminate\Console\Command; use GuzzleHttp\Client; use Goutte\Client as GoutteClient; class Spider extends Command { protected $signature = 'spider:run {url}'; protected $description = 'Run a simple spider.'; public function handle() { $url = $this->argument('url'); $client = new Client(); $goutte = new GoutteClient(); $response = $client->request('GET', $url); $crawler = $goutte->request('GET', $url); $crawler->filter('a')->each(function($node) { $link = $node->attr('href'); $this->info($link); }); } } ``` 在这个例子中,我们首先获取通过命令行传递的 URL,然后使用 Guzzle 发送一个 HTTP GET 请求获取页面内容。接下来,我们使用 Goutte 解析页面并找到其中的所有链接。 4. 运行命令 现在,您可以使用以下命令运行您的爬虫: ``` php artisan spider:run http://example.com ``` 这将输出页面中的所有链接。 这只是一个简单的示例,您可以根据需要扩展它。例如,您可以编写代码来在数据库中存储页面内容,或者使用更高级的爬虫技术来提取更有用的信息。 ### 回答2: 使用PHP的Laravel框架来构建一个简单爬虫可以使用Goutte和Guzzle这两个库来实现。下面是一个使用Goutte和Guzzle来构建简单爬虫的步骤。 首先,我们需要安装Goutte和Guzzle库。在Laravel项目的根目录下运行以下命令: ``` composer require goutte/goutte guzzlehttp/guzzle ``` 安装完成后,我们就可以开始编写爬虫代码了。首先在控制器中引入Goutte和Guzzle的命名空间: ``` use Goutte\Client; use GuzzleHttp\Client as GuzzleClient; ``` 接下来,我们可以在控制器中编写一个方法来实现爬虫功能。以下是一个简单的例子: ```php public function crawl() { // 创建一个Goutte的Client实例 $client = new Client(); // 使用Guzzle实现异步请求 $guzzleClient = new GuzzleClient(['timeout' => 60]); $client->setClient($guzzleClient); // 发起请求并获取响应 $crawler = $client->request('GET', 'http://example.com'); // 使用CSS选择器来提取需要的数据 $crawler->filter('h1')->each(function ($node) { echo $node->text()."\n"; }); } ``` 在上面的例子中,我们首先创建了一个Goutte的Client实例,并使用Guzzle作为底层的HTTP客户端。然后,我们发起了一个GET请求,并使用CSS选择器来过滤需要的数据。在这个例子中,我们提取了页面中的所有h1标签的文本内容,并打印出来。 最后,将上述代码放在你的控制器的方法中,并通过路由调用该方法即可执行爬虫功能。 以上就是使用PHP的Laravel框架、Goutte和Guzzle实现简单爬虫的基本步骤。当然,具体的爬虫功能和数据提取都可以根据实际需要进行更进一步的定制。 ### 回答3: 使用php laravel可以很方便地使用Goutte和Guzzle实现简单的爬虫功能。Goutte是一个用于Web页面抓取的PHP库,而Guzzle是一个功能强大的HTTP客户端。下面是一个使用Goutte和Guzzle实现简单爬虫的示例: 首先,确保已经安装了laravel,并且在composer.json中已经添加了goutte和guzzle依赖。 在需要使用爬虫的地方,可以创建一个新的控制器或者在已有的控制器中添加一个方法。在这个方法中,可以使用Goutte来载入目标网页并选择需要抓取的元素。 ```php use Goutte\Client; public function crawl() { $url = 'http://example.com'; // 要抓取的网页URL $client = new Client(); $crawler = $client->request('GET', $url); // 选择需要抓取的元素 $crawler->filter('h1')->each(function ($node) { echo $node->text()."\n"; }); // 使用Guzzle发送GET请求(可选) $client = new \GuzzleHttp\Client(); $response = $client->request('GET', $url); $body = $response->getBody(); // 进一步处理网页内容 } ``` 上述代码使用Goutte首先发送GET请求到指定的URL,然后使用filter方法选择需要抓取的元素,这里选择了`<h1>`标签,并使用each方法将抓取到的内容进行输出。 这种选择器的语法类似于jQuery,可以根据具体需求选择不同的元素进行抓取。 如果需要进一步处理网页内容,例如使用Guzzle发送HTTP请求,请先在控制器的顶部添加`use GuzzleHttp\Client;`,然后使用Guzzle的Client类发送HTTP请求。 最后,可以在路由中定义一个对应的路由,指向上面创建的控制器和方法,以便在浏览器中访问。 这只是一个简单的爬虫示例,实际的使用会根据具体需求进行更多的定制和功能拓展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值