Linux下cURL使用教程之四:curl基本使用实例

本篇主要结合编写的PHP程序实例对上篇curl基本使用进行具体说明。

环境搭建

HTTP服务器

建议Ubuntu+Apache+PHP,方便简洁:

sudo apt-get install apache2 php5

即可。

网页

以下为测试使用的服务器端代码,全部使用php实现。

  • curldemo.php,用于输出User-Agent、Referer、Cookie信息:
    <html>
    <head>
    <title>
    PHP Demo for curl basic usage By Stackeye
    </title>
    </head>
    <body>
    <?php
    echo "<strong>User-Agent:</strong><br>".$_SERVER['HTTP_USER_AGENT']."<br>\n";
    if(isset($_SERVER['HTTP_REFERER']))
    {
    echo "<strong>Referer:</strong><br>".$_SERVER['HTTP_REFERER']."<br>\n";
    }
    if(isset($_COOKIE['user']))
    {
    echo "<strong>Cookie:</strong><br>user=".$_COOKIE['user']."<br>\n";
    setcookie("user","",time()-1);
    }
    ?>
    <br><br><br>
    <a href="http://www.stackeye.com" target="_blank">Stackeye's Blog</a>
    </body>
    </html>
  • curlfollow.php,设置cookie后跳转至curldemo.php(使用js跳转,curl不会执行js函数,因此只有在浏览器中才会自动跳转):

    <?php
    setcookie("user","Stackeye");
    echo "<script>window.location =\"curldemo.php\";</script>";
    ?>
  • curl301.php,重定向至curldemo.php(header重定向后Referer字段为空,设置的cookie也不保存):

    <?php
    Header( "HTTP/1.1 301 Moved Permanently" ) ;
    Header( "Location: curldemo.php" );
    ?>
  • curldeny.php,对User-Agent/Referer/cookie有严格要求,才会显示最终内容:

    <html>
    <head>
    <title>
    CurlDeny-PHP Demo for curl basic usage By Stackeye
    </title>
    </head>
    <body>
    I'll deny all http requests,<br>
    except your User-agent contains "Mozilla/4.0",<br>
    your referer contains "www.baidu.com",<br>
    and your cookie user is "Stackeye"!<br>
    If you succeed,you will see "You finally get it"!<br><br>
    <?php
    if(strpos($_SERVER['HTTP_USER_AGENT'],"Mozilla/4.0")!==false)
    {
    echo "<strong>User-Agent:</strong><br>".$_SERVER['HTTP_USER_AGENT']."<br>\n";
    if(isset($_SERVER['HTTP_REFERER'])
    &&strpos($_SERVER['HTTP_REFERER'],"www.stackeye.com")!==false)
    {
    echo "<strong>Referer:</strong><br>".$_SERVER['HTTP_REFERER']."<br>\n";
    if(isset($_COOKIE['user'])&&strcmp($_COOKIE['user'],"Stackeye")==0)
    {
    echo "<strong>Cookie:</strong><br>user=".$_COOKIE['user']."<br>\n";
    setcookie("user","",time()-1);
    echo "You finally get it!<br>\n";
    }
    else
    echo "\n<br><br>I'm sorry but you are so close!<br>\n";
    }
    else
    echo "\n<br><br>I'm sorry but you are so close!<br>\n";
    }
    else
    echo "\n<br><br>I'm sorry but you are so close!<br>\n" ;
    ?>
    <br>
    <a href="http://www.stackeye.com" target="_blank">Stackeye's Blog</a>
    </body>
    </html>
  • setcookie.php,设置cookie的user字段,用以访问curldeny.php:

    <html>
    <head>
    <title>
    SetCookie-PHP Demo for curl basic usage By Stackeye
    </title>
    </head>
    <body>
    I just set the cookie,<br>
    with which you can get the curldeny.php.<br><br>
    <?php
    setcookie("user","Stackeye");
    ?>
    <br>
    <a href="http://www.stackeye.com" target="_blank">Stackeye's Blog</a>
    </body>
    </html>

以上PHP文件放入服务器根目录下即可(Ubuntu+Apache下默认为/var/www)。以下默认服务器地址为本机127.0.0.1。

curl访问任务

使用curl命令完成如下任务。

1. 伪装成Firefox访问curldemo.php

curl -s -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" \
-o curldemo.html http://127.0.0.1/curldemo.php

保存的html文件可以用浏览器打开查看。

2. 伪装来源页面为www.stackeye.com访问curldemo.php

curl -s -o curlex.html -e http://www.stackeye.com http://127.0.0.1/curldemo.php

3. 伪装成Firefox访问curlfollow.php,并保存cookie文件

然后使用保存的cookie文件,伪装成firefox,访问curldemo.php

curl -s -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" \
-o curlfollow.html -D cookie001.txt http://127.0.0.1/curlfollow.php
curl -s -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" \
-o curldemo.html -D cookie002.txt -b cookie001.txt http://127.0.0.1/curldemo.php

curldemo.html将显示user的值为Stackeye。

4. 伪装成Firefox并直接使用cookie值访问curldemo.php

cookie中user值为Stackeye,pass值为password

curl -s -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" \
-o curldemo.html -D cookie002.txt -b “user=Stackeye;pass=password” \
http://127.0.0.1/curldemo.php

curldemo.html将显示user的值为Stackeye,pass值为password。

5. 不自动跟踪重定向访问curl301.php

curl -v -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" \
-o curl301.html http://127.0.0.1/curl301.php

因为此时curl301.html文件内容为空,我们只能通过-v查看具体内容。
可以看到curl只访问了curl301.php

6. 自动跟踪重定向访问curl301.php

curl -s -L -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" \
-o curl301.html http://127.0.0.1/curl301.php

此时打开curl301.html会看到curldemo.php的内容,说明curl自动根据重定向去访问了curldemo.php。

7. 访问setcookie.php和curldeny.php,使curldeny.php显示“You finally get it!”

curl -s -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" \
-o setcookie.html -D cookie001.txt http://127.0.0.1/setcookie.php
curl -s -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" \
-o curldeny.html -e http://www.adeploy.com -D cookie002.txt -b cookie001.txt \
http://127.0.0.1/curldeny.php

打开curldeny.html文件,可看到“You finally get it!”信息。

总结

本篇主要是针对curl基本用法的实例以加深理解和掌握。实际操作中,登陆、发帖等操作还会用到提交表单相关操作,下篇将进行讲解。