guzzle

浅谈PHP组件、框架以及Composer

[亡魂溺海] 提交于 2020-11-22 20:58:45
本篇文章主要介绍了PHP组件、框架以及Composer,具有一定的学习价值,感兴趣的朋友可以了解一下。 什么是组件 组件是一组打包的代码,是一系列相关的类、接口和Trait,用于帮助我们解决PHP应用中某个具体问题。例如,你的PHP应用需要收发HTTP请求,可以使用现成的组件如guzzle/guzzle实现。我们使用组件不是为了重新实现已经实现的功能,而是把更多时间花在实现项目的长远目标上。 优秀的PHP组件具备以下特性: 作用单一:专注于解决一个问题,而且使用简单的接口封装功能 小型:小巧玲珑,只包含解决某个问题所需的最少代码 合作:PHP组件之间可以良好合作,组合在一起实现大型项目 测试良好:本身提供测试,而且有充足的测试覆盖度 文档完善:应该提供完善的文档,能让开发者轻易安装、理解和使用 组件 vs 框架 我们选择框架时,要为这个框架的工具投入很多,框架通常会提供大量工具,但却没有提供我们所需的某个工具时,痛苦就转嫁到我们头上,我们要寻找并集成自定义的PHP库。把第三方代码集成到框架中是件难事,因为第三方代码和框架可能没有使用相同的接口。 选择框架时,我们看中的是框架的未来,但是谁又能保证某个框架始终是完成某项工作最好的工具呢?存在多年的大型项目必须有好的表现,而且要时刻做好调整,如果选错了PHP框架,可能无法做到这一点。较旧的PHP框架可能由于缺乏社区支持而变慢或过时

Guzzle 使用说明

走远了吗. 提交于 2020-10-04 10:23:10
Guzzle 介绍 Guzzle 是一款简单、易用的 PHP HTTP 客户端。 它可以快速的集成到 WEB 项目中,帮助我们非常方便的发送 HTTP 请求。 Guzzle 特点 接口简单 支持使用 curl,PHP streams,sockets等各种方式。 支持同步和异步请求 遵循 PSR7 规范,可以集成其他的符合 psr7 规范的类库,自定义处理逻辑 安装 使用 composer 安装,非常方便 composer require --prefer-dist guzzlehttp/guzzle 快速入门 1.初始化客户端 use GuzzleHttp \ Client ; options = [ 'base_uri' => 'http://guzzle.testhttp.com' , 'connect_timeout' => 1 , 'timeout' => 3 , ]; $client = new Client($options); 2.发送body请求 $client->request( 'POST' , '/post' , [ 'body' => 'this is post body' ]); 3.发送表单请求 $client->request( 'POST' , '/post' , [ 'form_params' => [ 'user_id' => 1 , 'user

讲讲 Promise

醉酒当歌 提交于 2020-07-27 15:16:30
一、什么是 Promise 1.1 Promise 的前世今生 Promise 最早出现在 1988 年,由 Barbara Liskov 、 Liuba Shrira 首创(论文: Promises: Linguistic Support for Efficient Asynchronous Procedure Calls in Distributed Systems )。并且在语言 MultiLisp 和 Concurrent Prolog 中已经有了类似的实现。 JavaScript 中, Promise 的流行是得益于 jQuery 的方法 jQuery.Deferred() ,其他也有一些更精简独立的 Promise 库,例如: Q 、 When 、 Bluebird 。 # Q / 2010 import Q from 'q' function wantOdd () { const defer = Q.defer() const num = Math.floor(Math.random() * 10) if (num % 2) { defer.resolve(num) } else { defer.reject(num) } return defer.promise } wantOdd() .then(num => { log(`Success: ${num} is

讲讲 Promise

╄→гoц情女王★ 提交于 2020-07-27 11:50:12
一、什么是 Promise 1.1 Promise 的前世今生 Promise 最早出现在 1988 年,由 Barbara Liskov 、 Liuba Shrira 首创(论文: Promises: Linguistic Support for Efficient Asynchronous Procedure Calls in Distributed Systems )。并且在语言 MultiLisp 和 Concurrent Prolog 中已经有了类似的实现。 JavaScript 中, Promise 的流行是得益于 jQuery 的方法 jQuery.Deferred() ,其他也有一些更精简独立的 Promise 库,例如: Q 、 When 、 Bluebird 。 # Q / 2010 import Q from 'q' function wantOdd () { const defer = Q.defer() const num = Math.floor(Math.random() * 10) if (num % 2) { defer.resolve(num) } else { defer.reject(num) } return defer.promise } wantOdd() .then(num => { log(`Success: ${num} is

cURL request in Laravel

这一生的挚爱 提交于 2020-07-04 06:34:05
问题 I am struggling to make this cURL request in Laravel curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X GET http://my.domain.com/test.php I've been trying this: $endpoint = "http://my.domain.com/test.php"; $client = new \GuzzleHttp\Client(); $response = $client->post($endpoint, [ GuzzleHttp\RequestOptions::JSON => ['key1' => $id, 'key2' => 'Test'], ]); $statusCode = $response->getStatusCode(); But I am getting an error Class 'App\Http\Controllers\GuzzleHttp

Call to undefined function GuzzleHttp\Psr7\get_message_body_summary()

走远了吗. 提交于 2020-06-29 07:05:26
问题 I am trying to perform CURL get request in guzzlehttp to check if a user exists in a CRM. Whenever I try to perform the request I get the following error in the title, I haven't been able to find any resources online for this specific problem. Any ideas would be super helpful, if you require any additional info please let me know in the comments. Included packages: require(__DIR__ . "/../../vendor/autoload.php"); require_once(__DIR__ . "/../../helpers/Validation.php"); use Symfony\Component

How to access Guzzle's fullfilled response outside its scope?

只谈情不闲聊 提交于 2020-06-29 03:50:12
问题 I am new to guzzle package and I am sending async post requests through it using pool-promise method. Everything is well and good but once the request is fulfilled and response is received, I am trying to store some part of json response in an array $arr . $client = new Client(); $arr = []; $requests = function ($total) use ($client) { $request_headers = [ 'api_key' => config('app.wallet_server_api_key'), 'Content-Type' => 'application/x-www-form-urlencoded' ]; $form_params = [ 'accounts' =>

PHP GuzzleHttp. How to make a post request with params?

一世执手 提交于 2020-05-09 17:43:08
问题 How to make a post request with GuzzleHttp( version 5.0 ). I am trying to do the following: $client = new \GuzzleHttp\Client(); $client->post( 'http://www.example.com/user/create', array( 'email' => 'test@gmail.com', 'name' => 'Test user', 'password' => 'testpassword' ) ); But I am getting the error: PHP Fatal error: Uncaught exception 'InvalidArgumentException' with the message 'No method can handle the email config key' 回答1: Try this $client = new \GuzzleHttp\Client(); $client->post( 'http:

Laravel5.5 综合使用

戏子无情 提交于 2020-04-28 04:45:35
<blockquote>使用 Laravel5.5 开发一个自动交割的项目,把使用到的开源扩展包及特性整理起来,以供后续使用。</blockquote> <h2>一、安装IDE提示工具</h2> <p>Laravel IDE Helper 是一个极其好用的代码提示及补全工具,可以给编写代码带来极大的便利。</p> <h3>1、安装</h3> # 如果只想在开发环境安装请加上 --dev composer require barryvdh/laravel-ide-helper <p>安装 doctrine/dbal 「请装上它,在为模型注释字段的时候必须用到它」</p> # 如果只想在开发环境安装请加上 --dev composer require "doctrine/dbal: ~2.3" <p>详细安装方法,请参考这篇博文: <a href="https://laravel-china.org/articles/10172/laravel-super-good-code-prompt-tool-laravel-ide-helper" rel="nofollow noreferrer">Laravel 超好用代码提示工具 Laravel IDE Helper</a></p> <p>三个常用命令</p> <blockquote><ul> <li>php artisan ide