Ok guys basically i have this coded already but as 3 diffrent processes what i basically wanted to do is using curl check an external page title and also check if the page consist of a particular image on it like if the page has a logo.jpg on it or not an offcourse check if the host can open the page or not first... i wanted to do this by just opening the url once rather than 3 diffrent times is it possible ??
give us your code and we'll recommend the changes I should be easy, however to get the page using curl and then pass it to 3 functions and capture the result of each check. $page = myCurlFunctionToGetThePage($url); $logo = myTestForTheLogo($page); $test2 = myTest2($page); $test3 = myTest3($page); if ($logo && $test2 && $test3) doThis(); PHP:
i can PM you the code if you want.. but what u recommend wouldnt it still connect to the url and make 3 diffrent request ? Edit Now i get ur code basically what you are saying is to save the page in the variable and then use it throuh that ?? rite ? if so could you help me in code the function to get the page
You've got the idea. taken from http://www.php.net/manual/en/curl.examples-basic.php function myCurlFunctionToGetThePage($url) { // create curl resource $ch = curl_init(); // set url curl_setopt($ch, CURLOPT_URL, $url); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // $output contains the output string $output = curl_exec($ch); // close curl resource to free up system resources curl_close($ch); // pass the contents back to the main script return $output; } PHP:
In addition to sarahk's code: <?php function myCurlFunctionToGetThePage($url) { // create curl resource $ch = curl_init(); // set url curl_setopt($ch, CURLOPT_URL, $url); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // $output contains the output string $output = curl_exec($ch); // close curl resource to free up system resources curl_close($ch); // pass the contents back to the main script return $output; } //external page location $url = "http://website.com/page.htm"; //basic example, however would require regex for something more specific.. if(stristr(myCurlFunctionToGetThePage($url), 'logo.jpg')){ //the page has a logo.jpg... } ?> PHP:
If its a different site your trying to connect to each time then offcourse it has too but if its not simply place it within a variable so it connects once and stores the response/output within the variable, and then you can use the variable. Such as: <?php function myCurlFunctionToGetThePage($url) { // create curl resource $ch = curl_init(); // set url curl_setopt($ch, CURLOPT_URL, $url); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // $output contains the output string $output = curl_exec($ch); // close curl resource to free up system resources curl_close($ch); // pass the contents back to the main script return $output; } //external page location $response = myCurlFunctionToGetThePage("http://website.com/page.htm"); //basic example, however would require regex for something more specific.. if(stristr($response, 'logo.jpg')){ //the page has a logo.jpg... } //use $response if you need to do anything else... (no need to repeatedly be calling myCurlFunctionToGetThePage($url) if its the same $url ?> PHP:
thanks could you also help me with retrieving the page title.. btw rep added also if u want to confirm a page is online can i do it directly ?
Retrieving page title: <?php function myCurlFunctionToGetThePage($url) { // create curl resource $ch = curl_init(); // set url curl_setopt($ch, CURLOPT_URL, $url); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // $output contains the output string $output = curl_exec($ch); // close curl resource to free up system resources curl_close($ch); // pass the contents back to the main script return $output; } //external page location $response = myCurlFunctionToGetThePage("http://website.com/page.htm"); //use regex to find an retrieve the title from the html response.... preg_match('~<title>(.*)</title>~i', $response, $a); //place the title within an easily recognisable variable... $title = $a[1]; //display the title echo $title; ?> PHP:
Check if a page is online: <?php $headers = @get_headers("http://website.com/page.htm"); if (preg_match('~HTTP/([1-1\.]*) (200|302) (OK|Found)~', $headers[0])){ //page is online... } else { //page is not online... } ?> PHP:
Yes, here's all the above post(s) compiled in one, the site online/offline example i posted (get_headers) was because I believe its better than using cURL in this particular purpose - of checking if the page is online/offline. (or atleast my opinion). <?php function myCurlFunctionToGetThePage($url) { // create curl resource $ch = curl_init(); // set url curl_setopt($ch, CURLOPT_URL, $url); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // $output contains the output string $output = curl_exec($ch); // close curl resource to free up system resources curl_close($ch); // pass the contents back to the main script return $output; } $url = "http://website.com/page.htm"; //external page location $response = myCurlFunctionToGetThePage($url); //check if it contains logo.jpg if(stristr($response, 'logo.jpg')){ //the page has a logo.jpg... } //check if page is online... $headers = @get_headers($url); if (preg_match('~HTTP/([1-1\.]*) (200|302) (OK|Found)~', $headers[0])){ //page is online, theirfore retrieve title //use regex to find an retrieve the title from the html response.... preg_match('~<title>(.*)</title>~i', $response, $a); //place the title within an easily recognisable variable... $title = $a[1]; //display the title echo $title; } else { //page is not online... } ?> PHP:
thanks a lot but if website is offline it takes lot of time to load i guess it waits for it to timeout i am using this code in my current script to check if the page is online or not and it seems to be pretty fast function Visit($url){ $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";$ch=curl_init(); curl_setopt ($ch, CURLOPT_URL,$url ); curl_setopt($ch, CURLOPT_USERAGENT, $agent); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch,CURLOPT_VERBOSE,false); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch,CURLOPT_SSLVERSION,3); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE); $page=curl_exec($ch); //echo curl_error($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if($httpcode>=200 && $httpcode<300) return true; else return false; } PHP:
I've merged your Visit() function with myCurlFunctionToGetThePage(), so then you only need to connect once, follow this code: <?php function myCurlFunctionToGetThePage($url) { // create curl resource $ch = curl_init(); // set url curl_setopt($ch, CURLOPT_URL, $url); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // $output contains the output string $output = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // close curl resource to free up system resources curl_close($ch); if ($httpcode >= 200 && $httpcode < 300) { // pass the contents back to the main script return $output; } else { return false; } } $url = "http://website.com/page.htm"; //external page location $response = myCurlFunctionToGetThePage($url); //check if page is online... if ($response) { //page is online, theirfore retrieve title //use regex to find an retrieve the title from the html response.... preg_match('~<title>(.*)</title>~i', $response, $a); //place the title within an easily recognisable variable... $title = $a[1]; //display the title echo $title; //check if it contains logo.jpg if (stristr($response, 'logo.jpg')) { //the page has a logo.jpg... } } else { //page is offline... } ?> PHP:
get_headers() is fast if website is online i have put it at the top but when the web we are fetching us offline it just keeps loading is there a tweak to this or i am well with the visit function ??
oh thx for merging the functions but i think curl_setopt($ch, CURLOPT_TIMEOUT, 5); part is very important otherwise the process is still slow if page is offline would adding that damage anything ??