PHP cURL代码不起作用

时间:2016-12-10 04:40:04

标签: php curl

我正在尝试学习如何使用PHP cURL,我正在关注tutorial并使用Wamp。我要去localhost,无论我做什么改变,我都看不到代码的结果,我只看到了:

some of my php 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>

2 个答案:

答案 0 :(得分:2)

将文件扩展名更改为php,而不是html(即将其设置为.php)。从this answer引用@John Conde:

  

您无法在.html文件中运行PHP,因为服务器无法将其识别为有效的PHP扩展名,除非您告诉它。

因此,您可以修改Web服务器(例如Apache,IIS等)以使用HTML扩展名作为PHP文件处理文件。

此外,请确保使用以分号结尾的右括号结束options数组的赋值。有关数组的更多信息,请参阅php.net/array。所以这一行:

 $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,

应更新为:

 $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
);

您可以在this phpfiddle

上看到此问题

答案 1 :(得分:0)

你错过了&#39;)&#39;第18行(CURLOPT_URL => $url,

试试这个

<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>