简体   繁体   中英

Getting http header from a list of urls in a .txt file using PHP

Hello I have the following simple php script:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com");
curl_setopt($ch, CURLOPT_HEADER, true);  
curl_setopt($ch, CURLOPT_NOBODY, true); 
curl_exec($ch);
curl_close($ch);

and I want to do the same but not only to a url, but to a local list of urls contained in a.txt file

The file is something like this: http://... http://... http://...

but I have not find the way to make the options that I have in the original script apply to my list of urls. Any help would be appreciated. I'm a rookie rookie in php. Thank you

Assuming this is a file of space separated URLs, you could do it as follows:

// parse the file contents into a string variable 
$file_contents = file_get_contents($path_to_file);

// explode the contents into an array
$urls = explode(' ', $file_contents);

// init curl and set the options are the same for each URL
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);

// loop over the array
foreach ($urls as $url) {
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_exec($ch);
}

curl_close($ch);

If it isn't space separated just replace the first parameter of explode() with the correct separator.

EDIT:

So as mentioned in the comments - for a file with one URL per line you're best off using file()

// get all the URLs into an array  directly form the file
file($path_to_file);

// init curl and set the options are the same for each URL
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);

// loop over the array
foreach ($urls as $url) {
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_exec($ch);
}

curl_close($ch);

EDIT 2:

As pointed out in the comments, you can reuse curl by instantiating and configuring it outside the loop, saving you from having to set it up over and over again as the URLs are processed.

I've updated the code snippets accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM