Craigslist,CURL,简单的PHP DOM问题

时间:2010-08-09 05:38:56

标签: html dom curl craigslist

我正在使用CURL登录Craigslist以清除我发布的列表的状态。我遇到的问题是将HTML从CURL $输出传输到file_get_html。虽然Craigslist状态实际上嵌套在TR元素中,但我只是想测试最基本的函数来查看事物是否被传递(即链接抓取)。他们不是。

例如,这不起作用:

$cookie_file_path = getcwd()."/cookie.txt";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://accounts.craigslist.org/login?LoginType=L&step=confirmation&originalURI=%2Flogin&rt=&rp=&inputEmailHandle='.$email.'&inputPassword='.$password.'&submit=Log%20In');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.craigslist.org');

$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);

$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo $output;

//

include_once('simple_html_dom.php');
$html = file_get_html($output);
//find all links
foreach($html->find('a') as $element)
       echo $element->href . '<br>'; 

我知道该表达式有效,因为如果我输入“http://google.com”或其他内容,它会返回链接。

2 个答案:

答案 0 :(得分:1)

这是应该如何做的

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, 'http://www.sitename.com');  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);  
$str = curl_exec($curl);  
curl_close($curl);  

$html= str_get_html($str); 

答案 1 :(得分:0)

你不应该使用str_get_html而不是file_get_html吗? 因为$ output是一个字符串!