How to Download Bing Images Using PHP & cURL

hey guys what's going on with climate picky and in this video we're going to be creating a script to download images automatically from being search engine going to go to being dot-com and click on the images i'm going to enter nature for the keywords but you guys can enter anything you like so these are all the images i'm going to be downloaded to my computer already have the script created so I'm just going to demonstrate what it's going to do and a copy the script mean and i'm going to open up the command window here I PHP to run the script enter and then check the images folder and as you can see all the images are being downloaded automatically and i can double click on one of them and it's going to show the full resolution of the image so that's what we're going to be creating let's go ahead and get started on the script ok so i have an empty i created empty files called it downloading images that bhp so the first thing we're going to do here is we're going to copy this URL here but before we do that let's go ahead and click on filter your image size and click on extra large and for the type i want to select photograph now we can copy this URL up here into our files and just commented out for now and we would like that so for the keywords next time you're going to use nature and as you can see being created the keyword twice in the URL for some reason so you want to replace a guitar keyword and let's say that whole thing in a variable called URL and put it inside double quote and replace the nature with our keyword now we also want to replace the space with a plus sign because that's how the structure of URL works so i'm going to use a concatenate and use the str replace function to replace this place with a plus sign and close it here and I'm just going to copy the whole thing including a double quotes and replace this part as well ok so now our URL is ready to be loaded inside curl which is what we're going to be using to get all the data from the page and we're also going to use and curl to download the actual images so i'm going to go to new line here and and let's create our curl resource by using functional curl initialized then i'm going to set some curl options the first one is going to be for loading up our URL so going to provide the curl resource comma Carol off URL and then provide the URL that we're going to be loading which is up here next up I want to return the whole thing as data and stories inside the variable so i'm going to use this constant called the return transfer and set it to true then i'm going to store the output inside the variable called output using curl execute function and then I know what we want to do is curl close to close the total resource and that's it let's go ahead and test to see what the output looks like run the script and perfect it's sad loading the search page where we're ready to be where we're ready to match all the image names along with aurl's so we can download them but before we move on let's actually create a quick function out of this curl so that we don't have to repeat ourselves later on so the type function and maybe get and always going to have is a URL for the argument name and i'm going to close everything inside the curly brackets and then i'm going to return the output as i'm going to return the output from the function so now if I wanted to use this I was just any if i wanted to start at everything inside the output variable i would just type output equals get and then your else so that we don't have to type all this over again well we're going to be loaded on a URL for image downloading and now just accept it out again at the output everything still works just the same so okay so the next step is to we want to identify which parts are five images your l's that we're going to be matching so here I'm going to open up the developer tools and click on inspector actually first i'm going to click on one of these images and then i'm going to open up inspector and click on the inspector and then click on the image itself and then what I want to do here is I wanna added this HTML so i can copy that image URL and just go ahead and copy the URL and save it inside of your file for now now we're going to go back to the output so go ahead and run the file again and make sure the outside is in printed out then click you can call the right click on the window and go to view page source and then go ahead and search press ctrl find and search for that exact URL that you just copied so instead of this HTML source code that's going to show this URL and this is the URL that we want to match your right if you just click on it it's going to show the full size resolution of the image which is exactly what we want so for the matching we're going to copy this part which is how we're going to be which is how we're going to know that this is the image URL that we want to match so we want to grab all of this HTML here and copy inside of our file ok so i'm just going to close all of that inside a comment ok now we're ready to match all the image names using regular expression so drag match all going to go ahead and grab this HTML here and for the actual URL we don't need to be copied the whole thing which is going to say match anything and captured inside the regular expression group and i'm going to actually use single quotes so what I have to escape double quotes and don't forget the exclamation marks then i'm going to provide the output which is what we're going to be matching from and store everything inside your l matches then it can print our URLs matches one and one is a group where everything is being captured just to test out to see if everything works and let's go ahead and test this out okay close it by accident go ahead and run again ok so it's printed out the outputs i'm going to comment this out use source and perfect or all of the URLs all the images are being matched ok so now now that this works we are ready to use curls to download and all the images but before we do that we actually need the image name so we got to capture this last part of the URL so that when we're saving those images we cannot can have all the image names without the actual I long URL because it's not going to be valid if we're saving these images on a windows computer and this part is actually a little bit tricky because if we're going to be matching this last part from the /to the last part let me just show you something I'm gonna go to cycle the reg ex 101.com and i'm going to paste that image URL for the source code so here's what would happen if I would try to match this last part so if i just did that then I was expecting to match this last part from the /to the jpeg it actually grabbed the first the very first /a from the beginning and capture the whole URL we don't want that we don't want we just want this last part where it says greenwall nature wall that JPEG which is the image name so in order to do that we're going to match all the matches which have anything and and with a forward slash and then we're going to use a function called and to grab the last match of the array which is going to be this part here and now we're going to use another regular expression to match this part until jpg which is going to give us the image name ok this will make more sense as we do this so let's go ahead and do that now so first of all we're going to do is we want to loop through all of our image URL so let's go ahead and create a look for that at the counter and we're going to be counting until the end of our URL matches ok here's the part where we're going to be matching all over all of those side of the content which ends with a forward slash we're going to use a function called pregnant all and for the regular expression is going to be very simple match anything with a forward slash in it which ends with /and the content that we're going to imagine if URL underscore matches which is our URL of course provide the eye as well and then let's store everything inside the matches and let's test it out let's see what the first one looks like and can use dice it doesn't continue running ok so down here you can see that print from the first URL which is the blogspot one with green nature wallpapers it matched all the all the content with a forward question it and this is our last part which is what we want so we're going to use a function called end to grab that last part and we're going to name this we're going to save that in a lost part variable which makes thank matches Zira and now let's check out the last part I'm gonna comment this line out oops I need to die here ok so that's the last part and that's exactly what we're going to be using to match the image name ok so now that we have the last part we're ready to match the image name so let's use a fright match function put the last part inside of it and the last part is going to be followed by the image name which is which can be anything and then it's going to end with a JPEG and we're capturing everything inside the round brackets which is going to be our capture so for the content we're matching the actual URL again and we can store everything inside the match ok let's see if print our match one died ok so it's in fact mention the image name and now now that we have the image URLs and we have a there the image names we're ready to save them so go ahead and do that first of all I'm gonna make sure we're going to replace the plus signs from the image name with a dash which is going to be a lot more friendly when saving files to the computer so that's how we replace the plus for the dash using the STR STR replace function next up let's see the image URL in the variable as well which is going to be our URL matches 1i so that's going to be our image URL now now that we have the energy RL we actually want to load up the curl to get the image data so for that we are going to store that inside image data and here that's what's beautiful about the functions which is going to use our function get and we're going to provide the image URL and we'll have our image data which is ready to be saved on the computer now so now we have all information we have all the have the image data we have the image name and we have all that and now we're ready to save everything onto our computers so we're going to create a path where to say that my past is going to be c localhost PHP images so i go to this folder here this is my folder see localhost PHP i'm going to create a folder called images as well and this is the folder where is going to be saving all images make sure to provide your own folders that ok so now we're ready to save our images finally let's create a resource with a F open function and provide the local path along with the image name which is going to concatenate the image links to the source destination and we're going to use W to write the image means that we're going to use a fright function to write the actual image data which takes in the parameters resource which is our files and then the image data which we got from curl here and that will save the image and then we're just going to close the resource with half-closed ok so now everything should be working so let's go ahead and run the script and check the folder and in fact everything is working its downloading all image means automatically from being and if you check the image it's in fact that full resolution size and is down all these beautiful images automatically and that's it see if you guys found this video useful please like share subscribe and I'll see you next time by taking out