PHP cURL Tutorial - Learn PHP Programming

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on guys it's clever tikki and in this video we're going to learn about PHP C URL okay so C or L stands for client URL and it's a library that lets you make HTTP requests in PHP I'm just going to refer to it as curl because it's just easier for me to say that so we create first of all we create the curl resource with curl initialized function that's the first step the second step is to set curl options using peril set opt function and finally the third step is to run curl or execute the HTTP request with Carol execute function and there's a fourth step where we can close curl resource with curl underscore close function to free up the resources so that's how we create curl requests now let's go ahead and actually create one okay so I'm going to create the variable curl and initialize the resource now the curl is going to be datatype Carolla resource and when that happens when we have a curl resource we're able to use functions that are specifically designed for that resource like curl underscore set opt which is what I'm going to do here I'm going to curl set opt resource curl resource and then I'm going to use this constant curl opt underscore URL and provide actual URL which we which is going to be loaded on in the curl and then I'm going to execute the command with Carol exact and that's how we create Carol requests let's go ahead and test that okay so I've just loaded this file in a browser and happy Halloween Google is being loaded using our Carol request awesome so that's how you create curl requests now let's do something more fun I'm going to go ahead and go to amazon.com now and for the amazon.com they actually use the HTTPS protocol and in order for us to load this page we're going to set our curl opt SSL verify here to false so that's just one of the other curl options so I'm going to use the same function here provide our resource and then the constant is curl opt SSL verify here and then false and then here I'm going to load amazon.com and then go back to the browser and see what happens and now you can see that the PHP curl is being loaded and the amazon.com is being loaded dynamically now so that's how that works and if I comment out this option here nothing's going to show up because it because Amazon is using the HTTP protocol it's not going to work because it's it's a secure protocol so make sure to set this verify period to false if you know getting a load Amazon ok so what I'm going to do here is go back to amazon.com and we're going to do something fun now I'm going to go /as and then for the field keywords just type this in underscore as I mean form slash as and then forward slash field - keywords equals BC videogames 2016 that's how we create a search on Amazon using that kind of URL and what you're going to see here is just a bunch of video games show up on PC video games for 2016 and now what we're going to do is I'm going to go ahead and match every image of the game and then we're going to print them out in visually into our script so in order for us to do that I'm going to go ahead and open up my developer tools here in chrome and I'm going to go ahead and click on inspect and click on this image here and on the right hand side I'm going to get a bunch of code here and if I roll my mouse over this URL here this image is going to pop up so that's the URL that I want to match so I'm going to double click on this copy this whole URL okay um hold on one sec so I'm just going to copy that and then I can close the developer tools and go back to coding okay so for now I'm just going to leave this URL here for for matching later and there's a lot of option that you can set called Carol set opt curl if I set the option to curl opt underscore return transfer true and then I went back to the browser nothing is going to show up so what this option does here is we're able to store the result in a browser instead of I mean in a variable instead of instead of a automatically printed it in a browser so I can say a result equals Carol execute the curl and let's go ahead and also close Carol here free up system resources so now the result is going to be stored in the result so all the HTML instead of being printed out to the browser automatically is now stored inside the results so if I went ahead and echo that out it's going to print out the Amazon stuff here now let's actually go ahead and create that search URL instead so I'm just going to create on our variable here called URL and then copy this thing here and then going to close that in double quotes and then /s filled keywords equals and then I'm actually going to create another variable called search string it calls PC videogames 2016 for example and then I'm just going to put that inside of this string and for the URL I'm just going to put the URL variable here so now if i refresh this page nothing is showing up for some reason um okay I missed this misspell this field keywords make sure that spelled right so now this uh this page is being loaded with PC video games just like I wanted to so at this point I can use preg match all function to match all those image names so and that's what I'm gonna do here brag match all and then I'm just going to copy this URL that we copied error earlier and in this image URL basically anything this part is always going to stay the same because that's how amazon's what that's where amazon stores its images and this part is also going to stay the same so the only part is going to change is this identifier here which is a unique image name so i'm going to insert some regular expression here and I'm going to say match everything about spaces basically so this is the regular expression to match any character bought the space and don't worry about if you don't understand this for now I'm going to create another video about regular expressions later for now just understand that it's matching anything except spaces so it's going to match a bunch of these you RLS which are going to contain this URL here and at the end it's going to have the u.s. 160 underscore dot jpg so and for the matching I'm going to actually provide our kernel resource result and store that inside matches variable and now if we print it at that out matches let's see what happens okay it looks like it's matching all the image names now so I just went to view page source to view the array and actually this is exactly what I wanted so as you can see it's matching all these URLs just like I said with different image identifiers here so so what it does is just goes through the page and finds all those image URLs and matches all of them so next let's see so let's go ahead and print those images out to the browser now first of all I'm going to go ahead and make sure there's no duplicate values in this array because right now looks like there's some duplicates yep there's it's matching duplicate URLs which is not what we want so I'm going to use a going to say images equals array unique matches matches 0 and now if i refresh this page go to view page source messed up hmm matches Stratus huh images are a unique way wet print are matches okay so this one is matches zero then let's try this okay so images equals array unique matches zero oh forgot to print that okay now print the array back and okay so now the array is unique so it's printing it's matching and printing only the unique images and now also the array keys have been messed up so I'm going to use another function called array values to reset the array keys back to normal so the array keys that have been now reset to 0 from 26 and now we're ready to print out those images to the screen which is what I ultimately want to do here so I'm going to create a loop to loop through the array here okay and then I'm just going to copy and paste this piece of code here okay so what is going to do is going to loop through all these images and it's going to use the image IMG HTML tag and it's going to say image source equals whatever the URL of the image that we're matching is and if I go to back to the browser now hold on okay let's load the browser and it should print all those image names yep so that's what I wanted to do here I matched all the image names and then I've created a loop to loop through all those images and output them into the browser so that's one of the examples that you can what you can do with curl you can load some page on a website and then you can then you can match all those image names and format them format the content in whatever way you want so if I change the string here for example to movies movies 2015 for example refresh the page now all the movies have been matched and printed out because we've searched we search for the movies now so that's an example of curl and if you guys found this video useful please like share and subscribe and I'll see you next time clever check it out
Info
Channel: Clever Techie
Views: 123,380
Rating: undefined out of 5
Keywords: curl, php curl, php curl tutorial, php programming, learn php, php tutorial, php, php curl web scraping, php curl example, curl tutorial php, curl tutorial for beginners
Id: 1X2-UEUqrd8
Channel Id: undefined
Length: 13min 54sec (834 seconds)
Published: Mon Oct 31 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.