How to use a REST API from PHP using cURL | Full PHP cURL API tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ส.ค. 2024
  • Learn how to use a REST API from PHP using cURL.
    Support my channel: ☕ ko-fi.com/davehollingworth
    Full PHP APIs course: ➤ davehollingworth.net/phpapisy
    You'll learn the following:
    - The basics of making API requests in PHP
    - Encode and decode JSON data
    - Send request headers
    - Use different request methods (GET, POST, PATCH, DELETE etc.)
    - Respond to different HTTP status codes
    Code shown in the video:
    github.com/daveh/php-curl-cru...
    Resources shown in the video:
    www.php.net/manual/en/book.cu...
    docs.github.com/en/rest
    picocss.com/
    00:00 Introduction
    00:21 PHP cURL basics
    01:42 Add request headers
    04:14 Get a string response
    05:11 Decode JSON
    09:26 Endpoints with IDs
    12:35 Setting endpoints
    14:33 Multiple cURL options
    18:38 POST requests
    20:59 HTTP status code
    26:57 PATCH requests
    28:26 DELETE requests
    30:37 Outro
    #PHP #API #cURL
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 57

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

    Your tutorials are best, Dave!
    Keep up the good work!

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

    great tutorial, Dave! Was just googling for this earlier today, and you posted this tonight! Cheers

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

    This gives such clear descriptions and full explanations of what you are doing and why at every step. Wonderful content. Thank you!

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

    Clear, concise, spot on!,,,, thank you very much for passing it on..

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

    been struggling for days to just create a simple cURL POST api client, thankyou so much this has been the MOST informative breakdown of doing just that.
    and unlike most other online resources, you dont write the code line by line, you build the app as it should be built. Amazing work

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

    Enjoyed your video. I'm trying to get back into PHP after a very long hiatus (PHP 4) . Thanks for your videos for seeing what's new under the sun.

  • @lexiriam
    @lexiriam 3 หลายเดือนก่อน

    Your video tutorial is one of the best out there. Thank you for walking through and explaining the process in a slow, understandable method. I'm a subscriber!

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

    Easy to understand, Love it, Thank you Dave. 👍

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

    Thank you!! such a neat & clean tutorial.

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

    Just what I was looking for! Thanks a lot.

  • @piyushakoliya72
    @piyushakoliya72 6 หลายเดือนก่อน

    Amazing tutorial Dave.. very well explained..

  • @michellemartinez-vc1mj
    @michellemartinez-vc1mj ปีที่แล้ว

    Thank you for this explanation. This helped me so much.

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

    thanks for this tutorial it's super useful keep it up

  • @user-qs8be1dp5i
    @user-qs8be1dp5i 9 หลายเดือนก่อน

    Hello Sir, Thank you so much for the tutorials such a clear and steady voice to follow and learn things. I ever seen such a tutorial before. very useful👏👏👏👍👍👍

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

    You sir, display such a clean and proper way of teaching. It is such a relief to find videos with these topics on TH-cam that are spoken in proper English.
    Although I respect every individual that commits his/her time to produce free educational content, I am quite disappointed by the utter polution that exists on TH-cam nowadays. I am speaking of all the educational videos that seem to be produced with the proper use of the English language at first (e.g. English title, English thumbnail, English useraccount, English description) but once started only lead to disappointment because the speaker is someone who barely speaks understandable English.
    I say this with a certain level of understanding yet quite the amount of resent because it makes my personal user experience on TH-cam worse than it was before.
    Long story short, Dave Hollingworth, you are a godsend and I thank you for your videos!

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

    Nicely explained!

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

    you help me a lot! thank you !

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

    Another excellent video

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

    thanks for your effort this is helpful

  • @Iconic-scenes
    @Iconic-scenes ปีที่แล้ว

    Very good video, keep on the good work :)

  • @divineembassychildrenschoi6317
    @divineembassychildrenschoi6317 11 หลายเดือนก่อน +1

    Wow this was really great iam happy i found you , you just gained a new w in the chat

  • @b.sivakumar4328
    @b.sivakumar4328 ปีที่แล้ว +1

    Thank you

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

    dave bro, u are just awesome. u will be great in future. remember.

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

    Thank you.

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

    Thanks for the great content \o/

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

    this is gold

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

    Hi Dave, great video, thank you. was trying to show other details for the repository like the files. any hints would be appreciated. thanks

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

      You can adapt the code that shows a 404 to allow other URL segments, or use a third-party router if you have a lot of resource endpoints

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

    Hi Dave! I love your tutorials and I learned a lot from it. However, I am struggling in show.php because of undefined array in dl part.

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

      Try comparing your code to the source code linked to in the description, see if there's a typo in your code maybe

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

    Hi, A nice video, thank you. It helped a lot. How about making a video on retreaving data from an API with PHP/Curl and saving that data to a database, then retreaving the data and displaying it in HTML (for example on a users profile page). I'm thinking more of dynamic applications like Joomla.

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

      Hey! Have you been able to work this out? I'm currently working on a problem that would require the same. But from what i can tell, if you isolate the variable names, you can run insert statements into your db in a straightforward manner.

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

    Great tutorial … tried it to get data from Notion but struggling to display data from their array like you did with repository … can you make a tutorial for that?

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

      Not familiar with Notion but will take a look and add it to my list, thank you!

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

    Question: at 6:47 in running the foreach function, how do I begin troubleshooting if my code doesn't isolate the specific fields I'm interested in as yours did?

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

      Try printing out the contents of the various variables you're using before, during and after the loop using var_dump for example

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

      @@dave-hollingworth Lemme try that out. also some of the variables I'm looking for are nested within the primary array... How do I go about that?

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

      var_dump will print out the full contents of a multidimensional array, and you can call it from within a loop

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

      @@dave-hollingworth Hey Dave, I'm still having trouble isolating specific fields in my array response (my array response is very nested). For some reason it's not responding like in the tutorial with I identify the array indexes... I'm not sure why or whether it could be because of the structure of my array.

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

      @@lilliannjihia8557 Try debugging it by using print_r for various different levels of the array

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

    This is great. I've implemented this and your tutorial on creating a restful API in PHP, but I can't get cURL to connect to my API. For some reason, whether it's localhost or a proper server, because both are on the same server, it just won't connect. I think it could be some sort of firewall issue or maybe a PHP setting I don't know about. Any help on that would be appreciated.

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

      What happens when you try and connect, do you get any error messages at all?

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

      @@dave-hollingworth No, nothing, which is the most frustrating part! The cURL response is just empty with a 200 code and the script at the other end somehow gets called but also receives an empty request with no data. Bizarre!

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

      @@philadams9254 Try running the code from the video, there's a link in the description, see if that makes a difference - if so, try comparing yours to that code, see if you can spot a discrepancy that might be causing the error

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

      I feel so stupid. I'm getting a blank screen. On my wamp (windows 11 display_errors=On) as well as on my live server. I am at a complete loss here. PHP directly copied, with new github access token.

    • @matteonicoli
      @matteonicoli 11 หลายเดือนก่อน +1

      I solved it. I went to work to hasty (which of course is never good combined with late night coding). I dowloaded your code from github and used file init_curl. Did paste the access token header but forgot to paste the url. In my production curl project I got a blank screen as well, while everything worked fine in Postman. Until I added this line CURLOPT_SSL_VERIFYPEER => false, I received data (so relieved). Felt helpless for 2 days. In any case, Dave Hollingworth and many thanks. Will be watching a lot more excellent tutorials from you in the future.

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

    Hi , i have a little question , when i try to echo some entities Like in the vedio for fullname name and discerption i get the error Cannot access offset of type string on string anyone have any ideas? Thank you in advance .

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

      It means you're trying to use a string as an array: stackoverflow.com/a/20271518

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

    hi bro, I have a question. How to curl without reload page?

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

      cURL is called on the server, so you could call a page that used cURL using Ajax, or call the API directly from JavaScript

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

    What is IDE? Very simple and powerful)

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

      I use Atom - atom.io/

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

      @@dave-hollingworth as ATOM is getting retired this December, did you decide on a new IDE? im just starting to learn and would be interested on which alternative i might look for.

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

      @@battowsai I recommend Visual Studio Code, it's free and cross platform. (I'm still going to use Atom for my videos for the foreseeable future as it's very minimalist and works well for video tutorials, but if you're coding yourself, I would use VSCode)

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

      @@dave-hollingworth thank You for the answer. I guess i will try atom for the first project. This Tutorial will help me alot. The only problem i habe is that i Need to call three different rest api and compare the items.

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

    Bang kok lu mirip doctor strange