本篇主要结合编写的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>
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>