【发布时间】:2017-03-29 15:42:53
【问题描述】:
我想打开网站的所有页面 id 以 http://website.com/page.php?id=1 开头并以 id=1000 结尾
通过 preg_match 获取数据并将其记录在某处或 .txt 或 .sql
下面是我目前使用的 curl 函数,请告知完成这项工作的完整代码。
function curl($url)
{
$POSTFIELDS = 'name=admin&password=guest&submit=save';
$reffer = "http://google.com/";
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
$cookie_file_path = "C:/Inetpub/wwwroot/spiders/cookie/cook"; // Please set your Cookie File path. This file must have CHMOD 777 (Full Read / Write Option).
$ch = curl_init(); // Initialize a CURL session.
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url); // The URL to fetch. You can also set this when initializing a session with curl_init().
curl_setopt($ch, CURLOPT_USERAGENT, $agent); // The contents of the "User-Agent: " header to be used in a HTTP request.
curl_setopt($ch, CURLOPT_POST, 1); //TRUE to do a regular HTTP POST. This POST is the normal application/x-www-form-urlencoded kind, most commonly used by HTML forms.
curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS); //The full data to post in a HTTP "POST" operation.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // TRUE to follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set).
curl_setopt($ch, CURLOPT_REFERER, $reffer); //The contents of the "Referer: " header to be used in a HTTP request.
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); // The name of the file containing the cookie data. The cookie file can be in Netscape format, or just plain HTTP-style headers dumped into a file.
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); // The name of a file to save all internal cookies to when the connection closes.
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
【问题讨论】:
-
full code that will get this job done做了什么? -
我想打开以website.com/page.php?id=1 开头并以id=1000 结尾的网站的所有页面ID 通过preg_match 获取数据并将其记录在某处或.txt 或.sql
-
除了 curl() 函数,到目前为止你还尝试过什么?
-
@GeorgeSamarguliani 这个问题解决了吗?如果是这样,请用绿色勾号奖励 Subgeo 的答案。如果这不是您正在寻找的答案,请编辑您的问题并向 Subgeo 发表评论,指出某些事情仍然不太正确。绿色对勾表示 SO 回答者没有什么可以解决的,并向 SO 研究人员表示已提供正确答案。
标签: php mysql curl preg-match