使用cURL登录到远程网页后,我如何访问另一个网页并将其返回为字符串? [英] After using cURL to log into a remote webpage, how can I then access another webpage and return it as a string?

查看:107
本文介绍了使用cURL登录到远程网页后,我如何访问另一个网页并将其返回为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我对cURL很新鲜。我已经设法使用cURL登录到网页并发送POST数据,但一旦我登录,我想它加载各种网页下相同的 SESSION

Okay, I'm pretty new to cURL. I've managed to log into a webpage using cURL and sending POST data, but once I'm logged in, I'd like it to load various webpages under the same SESSION and extract data as a string, so that I can check if the webpage contains a specific string.

我将如何进行这项操作?

How would I go about doing this?

例如:

它登录 http://example.com/login.php

但登录后,我需要访问 http://example.com/site.php?page=news 并返回该页面的内容作为字符串。

But after logging on, I need it to visit http://example.com/site.php?page=news and return the contents of that page as a string.

推荐答案

处理状态管理。

在大多数情况下,这将通过cookie,因此您需要指示curl跟踪Cookie。

In most cases, this will be via a cookie, so you'll need to instruct curl to keep track of cookies.

类似:

curl_setopt($curl, CURLOPT_COOKIEJAR, '/tmp/cookies');  //where to write cookies received from server
curl_setopt($curl, CURLOPT_COOKIEFILE, '/tmp/cookies'); //where to read cookies to send in requests.

应该让你开始。

EDIT
Blatant复制/粘贴示例(从这里 - 发现与大约20秒的谷歌php curl登录)。这看起来是关于正确的:

EDIT Blatant Copy/Paste Example (from here - found with about 20 seconds of googling "php curl login"). This looks to be about right:

<?php 
$url ="http://login.yahoo.com/config/login?.src=ym&.intl=us&.partner=&.done=http%3A%2F%2Fmail.yahoo.com%2F"; 
$ch = curl_init();      
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, '/temp/cookie.txt'); 
curl_setopt ($ch, CURLOPT_POSTFIELDS, "login=emailid&passwd=password&&submit=Sign In"); 
ob_start();      
curl_exec ($ch); 
ob_end_clean();  
curl_close ($ch); 
unset($ch); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "/temp/cookie.txt"); 
curl_setopt($ch, CURLOPT_URL,"http://us.f515.mail.yahoo.com/ym/login?"); 
$result = curl_exec ($ch); 

curl_close ($ch); 
echo $result; 

这篇关于使用cURL登录到远程网页后,我如何访问另一个网页并将其返回为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆