1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need An Easy Code ...

Discussion in 'PHP' started by Bohra, Apr 22, 2010.

  1. #1
    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 ??
     
    Bohra, Apr 22, 2010 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #2
    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:
     
    sarahk, Apr 22, 2010 IP
    Bohra likes this.
  3. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #3
    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
     
    Last edited: Apr 22, 2010
    Bohra, Apr 22, 2010 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #4
    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:
     
    sarahk, Apr 22, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    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:
     
    danx10, Apr 23, 2010 IP
    Bohra likes this.
  6. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #6
    @danx10

    thanks but still i guess it would connect the site seprately for each function..
     
    Bohra, Apr 23, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    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:
     
    danx10, Apr 23, 2010 IP
  8. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #8
    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 ?
     
    Bohra, Apr 23, 2010 IP
  9. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #9
    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:
     
    danx10, Apr 23, 2010 IP
  10. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #10
    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:
     
    danx10, Apr 23, 2010 IP
    sarahk likes this.
  11. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #11
    ok thx so as per the codes uve posted we can say that i have to atleast connect to the site twice ??
     
    Bohra, Apr 23, 2010 IP
  12. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #12
    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:
     
    danx10, Apr 23, 2010 IP
  13. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #13
    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:
     
    Bohra, Apr 23, 2010 IP
  14. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #14
    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:
     
    Last edited: Apr 23, 2010
    danx10, Apr 23, 2010 IP
  15. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #15
    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 ??
     
    Bohra, Apr 23, 2010 IP
  16. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #16
    Check my reply before this one, regarding the merged functions.
     
    danx10, Apr 23, 2010 IP
  17. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #17
    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 ??
     
    Bohra, Apr 23, 2010 IP
  18. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #18
    No it should'nt just add it accordingly.
     
    danx10, Apr 23, 2010 IP
  19. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #19
    ok dude thanks a lot u have been a great help rep already added :)
     
    Bohra, Apr 23, 2010 IP
  20. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #20
    Also another thing, don't call me dude :)
     
    danx10, Apr 23, 2010 IP