How to call APIs from PHP: file_get_contents, cURL, Guzzle and SDKs

  Рет қаралды 40,069

Dave Hollingworth

Dave Hollingworth

Күн бұрын

PHP APIs course: ➤ davehollingworth.net/phpapisy
Support my channel: ☕ ko-fi.com/davehollingworth
There are several ways to make API calls from PHP. In this video we'll learn how to use each one, along with their advantages and disadvantages.
We'll cover the file_get_contents function, cURL, Guzzle and SDKs.
Details of the resources shown in the video and all the code used are below, including links to the RESTful APIs used.
Resources:
Testing API:
jsonplaceholder.typicode.com/
file_get_contents:
www.php.net/manual/en/functio...
www.php.net/manual/en/reserve...
www.php.net/manual/en/filesys...
cURL:
curl.se/
www.php.net/manual/en/book.cu...
www.php.net/manual/en/functio...
www.php.net/manual/en/functio...
Guzzle:
docs.guzzlephp.org/
SDKs:
stripe.com/docs/libraries#ser...
stripe.com/docs/api/customers...
Code shown in the video:
gist.github.com/daveh/c61fbe6...
API icon made by Smashicons (www.flaticon.com/authors/smas...) from Flaticon (www.flaticon.com)
00:00 Introduction
00:38 file_get_contents
07:06 cURL
14:22 Guzzle
19:30 SDKs
24:22 Summary
#PHP #API #REST #cURL #Guzzle #SDK

Пікірлер: 68
@blessed-favored
@blessed-favored 2 жыл бұрын
Thanks man. One of the best if not the best content on KZbin about APIs.
@jediampm
@jediampm 2 жыл бұрын
Hi, thanks for the video. Great explanation. Two things to point out: - to use curl with php, for dev (in your local PC) you need to install curl and respective php extension. - Guzzle behind the scenes will use curl if it install, if not you should had allow_url_fopen option enable.
@MrWilliamwright
@MrWilliamwright 2 жыл бұрын
Your courses are so good and full of useful info, thanks a lot.
@mahmoudusman5330
@mahmoudusman5330 2 жыл бұрын
Thank Mr Dave. Just the video I have been searching for. It's just as if you made this video because of me. Thank you
@JahidHasan-zn6xj
@JahidHasan-zn6xj 2 жыл бұрын
I really love your videos❤️ The way you explain and everything in details love it.👌
@jeanlegrandbokassa6688
@jeanlegrandbokassa6688 2 жыл бұрын
Thank you ! This video just come in the right time for me !!!
@Shiv-vb1re
@Shiv-vb1re 2 жыл бұрын
Thanks sir i bought the php course and completed it I learnt a lot
@Gricenich
@Gricenich 2 жыл бұрын
Thank you so much Dave, it's a great explanation, you saved my time!
@collenzaligway4458
@collenzaligway4458 2 жыл бұрын
Your content are great, hope you keep posting more cause its informative specially the OOP videos. Cheers mate
@renemayett5161
@renemayett5161 9 ай бұрын
Thanks a lot sir! Best regards from Mexico 👍
@isaacrafaelnunezservian708
@isaacrafaelnunezservian708 2 жыл бұрын
Thanks bro! Great translation!
@dawid_dahl
@dawid_dahl 2 жыл бұрын
Perfect, thank you so much!
@mehrdadriyahi2746
@mehrdadriyahi2746 2 жыл бұрын
Thanks for your wonderful video.
@RodrigoSotoCastro
@RodrigoSotoCastro 2 жыл бұрын
Nice video bro, thanks!
@gennadyga2257
@gennadyga2257 2 жыл бұрын
Very thanks!!!
@martingvideos
@martingvideos 2 жыл бұрын
GRACIAS!!!! Excelent!!
@KudoMarkos
@KudoMarkos 2 жыл бұрын
Thank you a lot
@blessed-favored
@blessed-favored 2 жыл бұрын
Kindly consider doing a video talking more about SDKs. For example how to write them.
@shubhamtiwary2524
@shubhamtiwary2524 2 жыл бұрын
Your courses are great. One request though, could you do a video on how to make our own apis in php.
@dave-hollingworth
@dave-hollingworth 2 жыл бұрын
Yep, working on it!
@vivamedia5958
@vivamedia5958 2 жыл бұрын
Awesome. Thanks If possible please more info about advantages and disadvantages of each one.
@dave-hollingworth
@dave-hollingworth 2 жыл бұрын
I did cover these in the video - using file_get_contents is part of base PHP, so you don't have to install anything, but it does require that setting to be enabled in php.ini. Also, to send headers you need to separate each one with newline characters that can be prone to errors. cURL is a PHP extension, so you have to make sure it's installed, but it's easier to use than file_get_contents. Guzzle is simpler to use as it's object oriented, but it does use cURL under the hood by default, so you have to make sure cURL is installed.
@vivamedia5958
@vivamedia5958 2 жыл бұрын
@@dave-hollingworth I'm sincerely thankful.
@nemac23
@nemac23 2 жыл бұрын
Awesome
@User97356
@User97356 Жыл бұрын
شكراً thank you
@sivaprasadsiva1770
@sivaprasadsiva1770 3 ай бұрын
which one is best for Android Application
@sampahemmanuel2643
@sampahemmanuel2643 Жыл бұрын
Awesome video. Please can the Guzzle be used for streaming.
@dave-hollingworth
@dave-hollingworth Жыл бұрын
docs.guzzlephp.org/en/5.3/streams.html
@RagHelen
@RagHelen 2 жыл бұрын
Dave, do you have any recommendations for books about the underlying logic and physics of PHP?
@dave-hollingworth
@dave-hollingworth 2 жыл бұрын
Not sure about books, but this article is very interesting: thephp.website/en/issue/how-does-php-engine-actually-work/
@rafaelfigueiredofurini6327
@rafaelfigueiredofurini6327 2 жыл бұрын
First of all, thanks, your videos are great....my question: How to debug my requests? In the video the are options to see http responses only(header and body)...
@dave-hollingworth
@dave-hollingworth 2 жыл бұрын
I'm glad you like the videos! To properly debug requests, I recommend using an API client like Postman (www.postman.com/) or HTTPie (httpie.io/)
@godofnothing520
@godofnothing520 2 жыл бұрын
Can you create a Laravel course on udemy, please? Your Codeigniter course on udemy is perfect, and your voice is kinda soothing. Also, your code is well structured.
@dave-hollingworth
@dave-hollingworth 2 жыл бұрын
Thank you for your kind words! I'll let you know if I do! :-)
@duyphi5035
@duyphi5035 11 ай бұрын
Thank you for your very detailed instructions. I have a question. I want to build a search form when I enter keywords into that form and press the search button it will call to another website and then I also want to use code to perform operations like searching for something in the form of that website instead of having to go to that website to enter it manually, then also use the code to automatically click the search button (submit button) in that website (if any) and then get that search results return to my localhost website. How can I do this?
@dave-hollingworth
@dave-hollingworth 11 ай бұрын
Not sure you can do that with PHP - there's a thread here that discusses it: stackoverflow.com/questions/55369863/how-can-i-use-php-to-prepopulate-a-form-on-or-send-data-to-a-3rd-party-website
@erichepperlewp
@erichepperlewp 2 жыл бұрын
00:05:00 - #file_get_contents(); ADVANTAGES: Part of PHP core; doesn't require extensions or external dependencies. DISADVANTAGES: Returns false for unsuccessful status request with no debugging data; Sending multiple request headers requires concatenation; Retrieving URLs requires allow_url_fopen() to be enabled which is commonly NOT enabled on "cheap shared hosting"
@ademineshat
@ademineshat 2 жыл бұрын
Thanks for sharing, but it's very disturbing having 6 advertisement interruptions with 2 ads in a row 🙄
@mathieustamm7395
@mathieustamm7395 10 ай бұрын
Hello, from what I understand, your function, with the file_get_content, encode. Personally, I would like to do the opposite, I have an api but special characters such as # or - and " are not displayed, I would even say that they are not taken into account. My question is, can I use your code to do the opposite of what you do?
@dave-hollingworth
@dave-hollingworth 10 ай бұрын
I'm not sure what you mean - the code in the video will call an API and display the response. If you're not seeing some characters, it could be a character encoding issue
@kevyyar
@kevyyar Жыл бұрын
how do you caches the api get responses?
@dave-hollingworth
@dave-hollingworth Жыл бұрын
There are many ways to cache in PHP - some libraries here: www.google.co.uk/search?q=php+cache+library
@devijv1441
@devijv1441 Жыл бұрын
Which a fastest library?
@dave-hollingworth
@dave-hollingworth Жыл бұрын
I haven't noticed a performance difference, but calling built-in functions will in theory be faster than using a library like Guzzle
@andresalvarez9394
@andresalvarez9394 2 жыл бұрын
Hi! First of all very usefull video Second I have a problem usign cURL, when I try to update the value of "title" I get this response string(103) "{ "userId": 1, "id": 1, "title": "quidem molestiae enim", "{\"title\":\"Updated title\"}": "" }" How can I solve this problem? Thanks!
@dave-hollingworth
@dave-hollingworth 2 жыл бұрын
It looks like you're escaping the updated title value - what code are you using to generate that JSON?
@chromeuser7363
@chromeuser7363 Жыл бұрын
I am trying to read file from api but neither file_get_contents nor curl are working on hosting website even if allow_url_fopen is On as shown in phpinfo.
@dave-hollingworth
@dave-hollingworth Жыл бұрын
Could be a network restriction - if the same code works locally, I would check with your hosting provider why it doesn't work there
@chromeuser7363
@chromeuser7363 Жыл бұрын
@@dave-hollingworth on file_get_contents I'm getting failed to open stream http 451 error. On cURL I am getting Moved 301 permanently error.
@dave-hollingworth
@dave-hollingworth Жыл бұрын
@@chromeuser7363 I've not seen 451 before - that suggests it's being blocked though: en.wikipedia.org/wiki/HTTP_451
@rosed4878
@rosed4878 12 күн бұрын
Im not sure why but i copied your code down from the 1:46 mark but all i got was bool(false)
@dave-hollingworth
@dave-hollingworth 12 күн бұрын
What is the line of code that's causing that error?
@cansahin2724
@cansahin2724 2 жыл бұрын
POSTing from a.php to b.php. I save it to the database and positive/negative results are returned. How can I print this result to my a.php file?
@dave-hollingworth
@dave-hollingworth 2 жыл бұрын
If you want to go back to a.php, you'll need to redirect using a "Location" header. To display the data, a.php will have to read the database.
@cansahin2724
@cansahin2724 2 жыл бұрын
@@dave-hollingworth Then this method doesn't work for me. Actually, what I want to do is: There is 1 order panel and orders will come to this panel from different sites. The people who make the sites should not see the database connection of the panel. After the POST process, the file on the server where the panel is located is also checked. And the result of this check needs to be transmitted from the panel to the form.
@cansahin2724
@cansahin2724 2 жыл бұрын
How can I do it ? I couldn't find a properly working example. Very happy if you help.
@dave-hollingworth
@dave-hollingworth 2 жыл бұрын
@@cansahin2724 If the value isn't sensitive data, then you could just include it in the query string when you redirect. Otherwise the calling site will need to request a URL that gives them this data somehow.
@cansahin2724
@cansahin2724 2 жыл бұрын
@@dave-hollingworth I can't send the result back.
@oussamalabied513
@oussamalabied513 2 жыл бұрын
in 9:34 i get :boolean false why
@dave-hollingworth
@dave-hollingworth 2 жыл бұрын
This usually happens if the hostname is unreachable (i.e. the domain name is incorrect - in the video it's "jsonplaceholder.typicode.com"
Securely Storing PHP Configuration Settings
7:20
Dave Hollingworth
Рет қаралды 5 М.
Whyyyy? 😭 #shorts by Leisi Crazy
00:16
Leisi Crazy
Рет қаралды 19 МЛН
When Steve And His Dog Don'T Give Away To Each Other 😂️
00:21
BigSchool
Рет қаралды 17 МЛН
Please be kind🙏
00:34
ISSEI / いっせい
Рет қаралды 58 МЛН
How to use a REST API from PHP using cURL | Full PHP cURL API tutorial
31:08
tRPC, gRPC, GraphQL or REST: when to use what?
10:46
Software Developer Diaries
Рет қаралды 69 М.
How To Call a REST API In Java - Simple Tutorial
29:14
Coding with John
Рет қаралды 255 М.
PHP cURL API Tutorial - Emailable API Integration - Full PHP 8 Tutorial
17:56
PHP cURL Tutorial - Learn PHP Programming
13:54
Clever Techie
Рет қаралды 154 М.
Learn JSON in 10 Minutes
12:00
Web Dev Simplified
Рет қаралды 3,1 МЛН
Guzzle php http client
12:47
htmllab
Рет қаралды 14 М.
ВЫ ЧЕ СДЕЛАЛИ С iOS 18?
22:40
Overtake lab
Рет қаралды 120 М.
iPhone 12 socket cleaning #fixit
0:30
Tamar DB (mt)
Рет қаралды 38 МЛН
Купил этот ваш VR.
37:21
Ремонтяш
Рет қаралды 240 М.
📦Он вам не медведь! Обзор FlyingBear S1
18:26
Хотела заскамить на Айфон!😱📱(@gertieinar)
0:21
Взрывная История
Рет қаралды 1,9 МЛН