0

I am trying to learn how to use PHP cURL and I am following a tutorial and while using Wamp. I am going to localhost and I never see the result of the code no matter the changes I do, all I see is:

some of my php code

This is my code:

<html>
<head>
</head>
<body>
<?php

    function curl($url){

        $options = Array(
            CURLOPT_RETURNTRANSFER => TRUE,  
            CURLOPT_FOLLOWLOCATION => TRUE,  
            CURLOPT_AUTOREFERER => TRUE, 
            CURLOPT_CONNECTTIMEOUT => 120,   
            CURLOPT_TIMEOUT => 120, 
            CURLOPT_MAXREDIRS => 10, 
            CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8",  
            CURLOPT_URL => $url, 
        $ch = curl_init();  
        curl_setopt_array($ch, $options);  
        $data = curl_exec($ch); 
        curl_close($ch);   
        return $data;   
    }

    function scrape_between($data, $start, $end){
        $data= stristr($data, $start);
        $data= substr($data, strlen($start)); 
        $stop= stripos($data, $end); 
        $data= substr($data, 0, $stop);
        return $data;
    }

    $scraped_page = curl("http://www.imdb.com");    // Downloading IMDB home page to variable $scraped_page
    $scraped_data = scrape_between($scraped_page, "<title>", "</title>");   // Scraping downloaded dara in $scraped_page for content between <title> and </title> tags

    echo $scraped_data; // Echoing $scraped data, should show "The Internet Movie Database (IMDb)"

    ?>
    </body>
    </html>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
evans
  • 58
  • 8
  • Welcome to SO. Please post the relevant text of the error within your post rather than in a link. – T-Heron Dec 10 '16 at 04:52
  • I am sorry but it is a mess so i uploaded an image, should i put it as code even though its not or just copy/paste it? @THeron – evans Dec 10 '16 at 04:56
  • It appears that the PHP code is not being processed as PHP code- is the file extension `.php`? If so you need to ensure that the web server (e.g. Apache, IIS, etc. is handling PHP files with the PHP executable... – Sᴀᴍ Onᴇᴌᴀ Dec 10 '16 at 05:53
  • `should [I put the error message] as code` (or other pre-formatted content) Put it in a block quote. If vertical alignment of characters isn't essential, do _not_ use (nest) code/pre-format: this allows "automatic" line wrap. (This may require appending two spaces to each line where the line break shall be preserved.) – greybeard Dec 10 '16 at 08:42
  • @SamOnela the file extension is .html – evans Dec 10 '16 at 09:26

2 Answers2

2

Change the file extension to php, not html (i.e. Make it end in .php). To quote @John Conde from this answer:

You can't run PHP in .html files because the server does not recognize that as a valid PHP extension unless you tell it to.

So you could modify the web server (e.g Apache, IIS, etc.) to process files with the HTML extension as PHP files.

Also, ensure that the assignment of the options array is ended with a closing parenthesis terminated with a semi-colon. For more information about arrays see php.net/array. So this line:

 $options = Array(
        CURLOPT_RETURNTRANSFER => TRUE,  
        CURLOPT_FOLLOWLOCATION => TRUE,  
        CURLOPT_AUTOREFERER => TRUE, 
        CURLOPT_CONNECTTIMEOUT => 120,   
        CURLOPT_TIMEOUT => 120, 
        CURLOPT_MAXREDIRS => 10, 
        CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8",  
        CURLOPT_URL => $url,

should be updated to:

 $options = Array(
        CURLOPT_RETURNTRANSFER => TRUE,  
        CURLOPT_FOLLOWLOCATION => TRUE,  
        CURLOPT_AUTOREFERER => TRUE, 
        CURLOPT_CONNECTTIMEOUT => 120,   
        CURLOPT_TIMEOUT => 120, 
        CURLOPT_MAXREDIRS => 10, 
        CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8",  
        CURLOPT_URL => $url
);

You can see this working on this phpfiddle

Community
  • 1
  • 1
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
  • Hey man,it worked thanks! i was so tired that i didnt realise that it was missing the parenthesis and semi-colon, but why didnt it work with .html? i thought u can have php code in html files – evans Dec 11 '16 at 12:14
  • See my updated explanation (with a link to [this answer](http://stackoverflow.com/a/11312349/1575353)) – Sᴀᴍ Onᴇᴌᴀ Dec 11 '16 at 18:06
0

You missed ')' in line 18 (CURLOPT_URL => $url,)

Try this

<html>
<head>
</head>
<body>
<?php

    function curl($url){

        $options = Array(
            CURLOPT_RETURNTRANSFER => TRUE,  
            CURLOPT_FOLLOWLOCATION => TRUE,  
            CURLOPT_AUTOREFERER => TRUE, 
            CURLOPT_CONNECTTIMEOUT => 120,   
            CURLOPT_TIMEOUT => 120, 
            CURLOPT_MAXREDIRS => 10, 
            CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8",  
            CURLOPT_URL => $url); 
        $ch = curl_init();  
        curl_setopt_array($ch, $options);  
        $data = curl_exec($ch); 
        curl_close($ch);   
        return $data;   
    }

    function scrape_between($data, $start, $end){
        $data= stristr($data, $start);
        $data= substr($data, strlen($start)); 
        $stop= stripos($data, $end); 
        $data= substr($data, 0, $stop);
        return $data;
    }

    $scraped_page = curl("http://www.imdb.com");    // Downloading IMDB home page to variable $scraped_page
    $scraped_data = scrape_between($scraped_page, "<title>", "</title>");   // Scraping downloaded dara in $scraped_page for content between <title> and </title> tags

    echo $scraped_data; // Echoing $scraped data, should show "The Internet Movie Database (IMDb)"

    ?>
    </body>
    </html>
ashkufaraz
  • 5,179
  • 6
  • 51
  • 82