Help with curl_setopt

Discussion in 'PHP' started by DjZoC, May 24, 2010.

  1. #1
    can someone fix this command ...

    <?php
     $url = 'http://www.google.com';
    
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_USERAGENT, 'Firefox (WindowsXP) - Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');
     curl_setopt($ch, CURLOPT_URL,$url);
     curl_setopt($ch, CURLOPT_FAILONERROR, true);
     curl_setopt($ch, CURLOPT_AUTOREFERER, true);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
     curl_setopt($ch, CURLOPT_TIMEOUT, 30);
     $html= curl_exec($ch);
    
     curl_close($ch);
    
     if (!$html) {
      echo "<br />cURL error number:" .curl_errno($ch);
      echo "<br />cURL error:" . curl_error($ch);
      exit;
     }
    
     $title = preg_match('/<title>(.*)<\/title>/', $html, $html);
    
     echo $title;
    ?>
    PHP:
    on my website is show me 1 is not show the title of that page
     
    DjZoC, May 24, 2010 IP
  2. shubhamjain

    shubhamjain Active Member

    Messages:
    215
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #2
    For cURL, I recommend to use a common function.

    function cURL($url, $header=NULL, $cookie=NULL, $post_data=NULL)
    {
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_HEADER, $header);
    	curl_setopt($ch, CURLOPT_NOBODY, $header);
    	curl_setopt($ch, CURLOPT_URL, $url);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    	curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    	curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    	if ($post_data) {
    		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    		curl_setopt($ch, CURLOPT_POST, 1);
    		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    	}
    	$result = curl_exec($ch);
    
    	if ($result) {
    		return $result;
    	} else {
    		return curl_error($ch);
    	}
    	curl_close($ch);
    }
    
    Code (markup):
    You can use it in following way -

    $a = cURL("http://google.com");
    echo $a;
    
    Code (markup):
     
    shubhamjain, May 25, 2010 IP
  3. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #3
    It looks like it is your use of the above function that is causing the problem.

    The return value of the preg_match function is the number of matches; because of the way it works it will be either 0 or 1.

    You should use the contents of the third parameter.

    I.E

    # To inspect its contents use print_r($html);
    
    print $html[1];
    PHP:
     
    lukeg32, May 25, 2010 IP
  4. jaholden

    jaholden Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You should also add the 'm' and 'i' flags to the preg_match expression so that it a) matches across multiple lines and b) is case insensitive. Otherwise, you would not match the title if there was a line break between the <title> and </title> tags, and you would miss capitalised tags too, such as <TITLE>.

    If you're wanting to extract data from HTML pages you might want to look at the DomDocument functions.
     
    jaholden, May 25, 2010 IP