2012-09-26 64 views
0

现在,我登录到一个网站,该网站在页眉中显示了我的用户名,表明我已登录。用户信息在抓取时丢失

现在,当我尝试抓取该网页并在我的m/c上显示结果时,页眉显示“Sign-in”,表示我需要登录。

我想我缺少一些cookie信息,在我需要考虑的一些cookie信息中。

有没有什么方法可以让我读取cookies。

卷曲代码:

function getString($url) { 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
    curl_setopt($ch, CURLOPT_COOKIESESSION, true); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 
    $response = curl_exec($ch); 
    curl_close($ch); 
    return $response; 
} 
+0

你考虑过使用CURL吗? – Baba

+0

我已经使用它,但仍然是相同的结果。 –

+0

什么是*“我的m/c”*?另外* simplehtmldom *甚至不适合解析html,但它也不适合拼凑。使用PHP设置您的HTTP请求(请参阅HTTP上下文选项)或使用curl来代替,然后请求您自己的HTML数据并将其传递给HTML解析器。 – hakre

回答

1

您的代码不工作becasue完整路径Cookie路径,并确保cookie.txt是可写的

尝试

var_dump(getString("http://google.com")); 

    function getString($url) { 
    $ch = curl_init(); 
    $cookie = __DIR__ . '/cookie.txt' ; 
    touch($cookie); 

    if(!is_writable($cookie)) 
    { 
     die("Can't write to cookie"); 
    } 

    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
    curl_setopt($ch, CURLOPT_COOKIESESSION, true); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie); 
    curl_setopt($ch, CURLOPT_COOKIEFILE,$cookie); 
    $response = curl_exec($ch); 
    curl_close($ch); 
    return $response; 
} 

cookie.txt输出

# Netscape HTTP Cookie File 
# http://curl.haxx.se/rfc/cookie_spec.html 
# This file was generated by libcurl! Edit at your own risk. 

.google.com TRUE / FALSE 1411737249 PREF ID=ff7979720d6a1237:FF=0:TM=1348665249:LM=1348665249:S=bRYSIBSW9Cd7PKOr 
#HttpOnly_.google.com TRUE / FALSE 1364476449 NID 64=tcm3RUM8R_1ch9eD6tuFi4lObBjSNdxqwMHbpchYCQoUpghIjZbiNw8AdAm0buTAVF0SqUsZsYEs7PAWhJdhutO11EQ9y8iXwuQ9dsPmdWlt86BAa7hxRqQcjSoX9Bep 
.google.com.ng TRUE / FALSE 1411737252 PREF ID=9428863ec2e741f5:FF=0:TM=1348665252:LM=1348665252:S=s7wtyWMM9OnRYoE4 
#HttpOnly_.google.com.ng TRUE / FALSE 1364476452 NID 64=Gyszb-4_10nzvSU6kGzBj5UQRTnB7purbAH0reBytKi_pn9m3R-0BXGBEjrkmMBmOYfFpfIQOYLaCgi5LfKOIcnPCrTpTpV9LVld-Xf9pq7U7W5QaZ63a_yHIG9Vmcir 
+0

请不要复制内容。相反,有助于指出现有资源,那将非常棒。谢谢! – hakre

+0

@hakra感谢您的建议......已修复了他的代码 – Baba

+1

那么在调试会话中玩得很开心;)。 – hakre