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

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ค. 2024
  • 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

  • @jediampm
    @jediampm 2 ปีที่แล้ว +7

    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.

  • @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

  • @MrWilliamwright
    @MrWilliamwright 2 ปีที่แล้ว +4

    Your courses are so good and full of useful info, thanks a lot.

  • @blessed-favored
    @blessed-favored 2 ปีที่แล้ว +2

    Thanks man. One of the best if not the best content on TH-cam about APIs.

  • @JahidHasan-zn6xj
    @JahidHasan-zn6xj 2 ปีที่แล้ว +1

    I really love your videos❤️
    The way you explain and everything in details love it.👌

  • @Gricenich
    @Gricenich 2 ปีที่แล้ว

    Thank you so much Dave, it's a great explanation, you saved my time!

  • @jeanlegrandbokassa6688
    @jeanlegrandbokassa6688 2 ปีที่แล้ว +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

  • @collenzaligway4458
    @collenzaligway4458 2 ปีที่แล้ว

    Your content are great, hope you keep posting more cause its informative specially the OOP videos. Cheers mate

  • @renemayett5161
    @renemayett5161 10 หลายเดือนก่อน

    Thanks a lot sir! Best regards from Mexico 👍

  • @isaacrafaelnunezservian708
    @isaacrafaelnunezservian708 2 ปีที่แล้ว

    Thanks bro! Great translation!

  • @mehrdadriyahi2746
    @mehrdadriyahi2746 2 ปีที่แล้ว

    Thanks for your wonderful video.

  • @dawid_dahl
    @dawid_dahl 2 ปีที่แล้ว

    Perfect, thank you so much!

  • @RodrigoSotoCastro
    @RodrigoSotoCastro 2 ปีที่แล้ว

    Nice video bro, thanks!

  • @gennadyga2257
    @gennadyga2257 2 ปีที่แล้ว +1

    Very thanks!!!

  • @martingvideos
    @martingvideos 2 ปีที่แล้ว +1

    GRACIAS!!!! Excelent!!

  • @shubhamtiwary2524
    @shubhamtiwary2524 2 ปีที่แล้ว +6

    Your courses are great. One request though, could you do a video on how to make our own apis in php.

  • @KudoMarkos
    @KudoMarkos 2 ปีที่แล้ว +1

    Thank you a lot

  • @blessed-favored
    @blessed-favored 2 ปีที่แล้ว

    Kindly consider doing a video talking more about SDKs. For example how to write them.

  • @vivamedia5958
    @vivamedia5958 2 ปีที่แล้ว +2

    Awesome. Thanks
    If possible please more info about advantages and disadvantages of each one.

    • @dave-hollingworth
      @dave-hollingworth  2 ปีที่แล้ว +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 ปีที่แล้ว +2

      @@dave-hollingworth I'm sincerely thankful.

  • @User97356
    @User97356 ปีที่แล้ว

    شكراً thank you

  • @nemac23
    @nemac23 2 ปีที่แล้ว

    Awesome

  • @godofnothing520
    @godofnothing520 2 ปีที่แล้ว +1

    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! :-)

  • @sampahemmanuel2643
    @sampahemmanuel2643 ปีที่แล้ว +1

    Awesome video. Please can the Guzzle be used for streaming.

  • @RagHelen
    @RagHelen 2 ปีที่แล้ว +1

    Dave, do you have any recommendations for books about the underlying logic and physics of PHP?

    • @dave-hollingworth
      @dave-hollingworth  2 ปีที่แล้ว +1

      Not sure about books, but this article is very interesting: thephp.website/en/issue/how-does-php-engine-actually-work/

  • @rafaelfigueiredofurini6327
    @rafaelfigueiredofurini6327 2 ปีที่แล้ว +1

    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/)

  • @duyphi5035
    @duyphi5035 ปีที่แล้ว

    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"

  • @sivaprasadsiva1770
    @sivaprasadsiva1770 4 หลายเดือนก่อน

    which one is best for Android Application

  • @mathieustamm7395
    @mathieustamm7395 11 หลายเดือนก่อน

    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  11 หลายเดือนก่อน

      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

  • @ademineshat
    @ademineshat 2 ปีที่แล้ว

    Thanks for sharing, but it's very disturbing having 6 advertisement interruptions with 2 ads in a row 🙄

  • @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?

  • @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

  • @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 หลายเดือนก่อน

    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  หลายเดือนก่อน

      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"