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

Поделиться
HTML-код
  • Опубликовано: 13 июн 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.

  • @blessed-favored
    @blessed-favored Год назад +2

    Thanks man. One of the best if not the best content on RUclips about APIs.

  • @MrWilliamwright
    @MrWilliamwright 2 года назад +4

    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 года назад +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 9 месяцев назад

    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!

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

  • @gennadyga2257
    @gennadyga2257 2 года назад +1

    Very thanks!!!

  • @KudoMarkos
    @KudoMarkos 2 года назад +1

    Thank you a lot

  • @martingvideos
    @martingvideos 2 года назад +1

    GRACIAS!!!! Excelent!!

  • @blessed-favored
    @blessed-favored Год назад

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

  • @User97356
    @User97356 Год назад

    شكراً thank you

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

  • @nemac23
    @nemac23 2 года назад

    Awesome

  • @sivaprasadsiva1770
    @sivaprasadsiva1770 3 месяца назад

    which one is best for Android Application

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

  • @sampahemmanuel2643
    @sampahemmanuel2643 Год назад +1

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

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

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

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

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

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

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

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

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

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

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

  • @rosed4878
    @rosed4878 10 дней назад

    Im not sure why but i copied your code down from the 1:46 mark but all i got was bool(false)

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