我是新手使用cURL,很难找到好的资源 . 我要做的是登录到远程站点,让curl执行登录表单,然后发回成功 .
我的代码似乎不起作用,只试图显示网站的主页 .
$username="mylogin@gmail.com";
$password="mypassword";
$url="http://www.myremotesite.com/index.php?page=login";
$cookie="cookie.txt";
$postdata = "email=".$username."&password=".$password;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
echo $result;
curl_close($ch);
我究竟做错了什么 . 在此工作之后,我想重定向到另一个页面并从我的网站获取内容 .
5 回答
我已经让它好好享受了一段时间,但后来再次重访 . 由于这个问题定期查看 . 这最终是我最终使用的对我有用的东西 .
Update: This code was never meant to be a copy and paste. It was to show how I used it for my specific use case. You should adapt it to your code as needed. Such as directories, vars etc
我有同样的问题,我找到了这个答案on this website .
我稍微改了一下(最后一行的curl_close)
我想这就是你要找的东西 . 我是对的吗?
还有一个有用的相关问题 . 关于如何在cUrl中保持会话活动:https://stackoverflow.com/a/13020494/2226796
查看登录页面的来源 . 查找
form
HTML标记 . 在该标记内的内容看起来像action=
将该值用作$url
,而不是表单本身的URL .此外,当您在那里时,请验证输入框是否已列为您列出的内容 .
例如,基本登录表单看起来类似于:
使用上面的表单作为示例,将
$url
的值更改为:验证
$postdata
中列出的值:它应该工作得很好 .
这就是我在ImpressPages中解决这个问题的方法:
巴拿马杰克示例对我不起作用 - 给出致命错误:调用未定义的函数build_unique_path() . 我使用了这段代码 - (更简单 - 我的意见):