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

Ғылым және технология

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
    @ilyamur8852 жыл бұрын

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

  • @tonyhomfray
    @tonyhomfray2 жыл бұрын

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

  • @novakjetil
    @novakjetil2 жыл бұрын

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

  • @rshelfo1
    @rshelfo12 жыл бұрын

    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

  • @BoneyardHauntedHouse
    @BoneyardHauntedHouse2 жыл бұрын

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

  • @chadmasters3935
    @chadmasters39352 жыл бұрын

    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.

  • @bugsbunny9965
    @bugsbunny99652 жыл бұрын

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

  • @lexiriam
    @lexiriam3 ай бұрын

    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!

  • @SLCODEGEN
    @SLCODEGEN2 жыл бұрын

    Thank you!! such a neat & clean tutorial.

  • @athithyaparamesh8251
    @athithyaparamesh82512 жыл бұрын

    Just what I was looking for! Thanks a lot.

  • @piyushakoliya72
    @piyushakoliya726 ай бұрын

    Amazing tutorial Dave.. very well explained..

  • @mullertm4778
    @mullertm47782 жыл бұрын

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

  • @michellemartinez-vc1mj
    @michellemartinez-vc1mj Жыл бұрын

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

  • @craigc5879
    @craigc58792 жыл бұрын

    Another excellent video

  • @user-qs8be1dp5i
    @user-qs8be1dp5i9 ай бұрын

    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 Жыл бұрын

    You sir, display such a clean and proper way of teaching. It is such a relief to find videos with these topics on KZread 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 KZread 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 KZread worse than it was before. Long story short, Dave Hollingworth, you are a godsend and I thank you for your videos!

  • @dave-hollingworth

    @dave-hollingworth

    Жыл бұрын

    Thank you so much for your kind words Johnny!

  • @Iconic-scenes
    @Iconic-scenes Жыл бұрын

    Very good video, keep on the good work :)

  • @sorasoralee2127
    @sorasoralee21272 жыл бұрын

    you help me a lot! thank you !

  • @PrivateNaelyan
    @PrivateNaelyan10 ай бұрын

    Nicely explained!

  • @themonster4759
    @themonster4759 Жыл бұрын

    thanks for your effort this is helpful

  • @divineembassychildrenschoi6317
    @divineembassychildrenschoi631711 ай бұрын

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

  • @JuanBotes
    @JuanBotes2 жыл бұрын

    Thanks for the great content \o/

  • @SkLzaGaming
    @SkLzaGaming2 жыл бұрын

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

  • @b.sivakumar4328
    @b.sivakumar4328 Жыл бұрын

    Thank you

  • @tazefaze
    @tazefaze2 жыл бұрын

    Thank you.

  • @taelabaho
    @taelabaho2 жыл бұрын

    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
    @deesolvero55482 жыл бұрын

    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
    @brendanhedges75912 жыл бұрын

    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
    @gunnarnopens22442 жыл бұрын

    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 жыл бұрын

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

  • @philadams9254
    @philadams9254 Жыл бұрын

    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

    Жыл бұрын

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

  • @philadams9254

    @philadams9254

    Жыл бұрын

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

    Жыл бұрын

    @@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 ай бұрын

    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.

  • @lilliannjihia8557
    @lilliannjihia85572 жыл бұрын

    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

    Жыл бұрын

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

  • @toano8855
    @toano88552 жыл бұрын

    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

  • @yousseflahmadi7665
    @yousseflahmadi76652 жыл бұрын

    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

  • @gennadyga2257
    @gennadyga22572 жыл бұрын

    What is IDE? Very simple and powerful)

  • @dave-hollingworth

    @dave-hollingworth

    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

Келесі