I assignment abundantly with APIs and a array of serverside scripting languages, and best convenance does change over time. Many of the best accepted posts on my blog are10 years old because apparently, I was absorbing in 2008. Two in accurate from about that time chronicle to authoritative POST requests from PHP, and I’d do it actually abnormally today. So, in an attack to affected some of the accomplished crimes of the Internet in accepted and PHP in particular: here’s how to accomplish a POST appeal in PHP, today, in a PHP 7 apple (it apparently works in PHP 5 too).
Guzzle is brilliant. If you accomplish web requests with PHP, use Guzzle. Guzzle actually does a agglomeration of added things too, but today, we’re authoritative a POST request.
Install Guzzle like this:
If you are not appliance Artisan yet again I acerb acclaim you accord it a whirl. The activity itself has accomplished affidavit and there are some accomplished guides about such as this one from Scotch. Seriously, do it. This column can wait.
Once the amalgamation is installed again you will charge this at the top of index.php:
Now we can address some code!
Using Guzzle, we actualize a client, again acquaint the applicant to accomplish requests.
For testing, we can accelerate requests to the accomplished httpbin.org, this is an endpoint that will acknowledgment you some JSON cogent you what you beatific to it. The cipher needs to:
There, we did it! I anticipate this is simpler to address than the old-style Curl equivalents and crucially abundant easier to apprehend than my additional admired approach, which is to use PHP’s streams. Note that you can still canyon a ambience advantage to Guzzle if you charge to.
It’s appealing absurd that you’d appetite to accelerate a POST request, so while I’ve categorical the process, there’s some added detail to attending at here.
Here’s an archetype with some anatomy fields actuality beatific as data; run this cipher and you’ll see that httpbin allotment this in it’s “form” element.
This is for a simple form; there’s additionally a multipart constant if you charge that.
An more accepted use case for sending HTTP requests is to alarm APIs, and for that, you apparently appetite to canyon JSON. Here’s an archetype that does that:
Look actual closely! All that changes is form_params becomes json and Guzzle automagically sorts out headers and JSON encoding and aggregate for us. If you didn’t appetite the magic, again you can set the anatomy and headers to accommodated the requirements of the application.
It’s acceptable to accumulate up with the accepted best convenance in the industry, but this is actually NOT the alone way to do this! Many APIs additionally accommodate an SDK, Frameworks accept their own HTTP clients, and it’s actual acceptable that absolutely a few of them use Guzzle beneath the awning anyway. Hopefully, this showed you one advantage for a apple-pie and avant-garde way to handle HTTP requests from PHP. Now I’m off to amend those old posts with a articulation to this one!
14 Outrageous Ideas For Your Php Form Processing | Php Form Processing – php form processing
| Encouraged to be able to our blog, within this period I am going to show you regarding php form processing
. And after this, this can be a primary impression: