donglu9134 2014-10-06 21:53
浏览 113
已采纳

在PHP上接受使用CURL的cookie

I'm trying to run a log in script over my Centos machine. What the script does is logging in with a username and password to a 3rd party site and gets the page contents.

Although the script works perfectly in my PC (XAMPP at Windows), in my Centos box it seems not work. After logging in, it keeps redirecting to the log in page (although the log in succeed). Here is the code:

function request($url,$post)
{
$ch = curl_init();
$curlConfig = array(
    CURLOPT_URL            => $url,
    CURLOPT_POST           => 1,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_COOKIEFILE => 'cookies.txt',
    CURLOPT_COOKIEJAR => 'cookies.txt',
    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"',
    CURLOPT_FOLLOWLOCATION => 1,
    CURLOPT_REFERER => $url,
    CURLOPT_POSTFIELDS     => $post
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
echo request('site.com/login.php',array("username" => "test", "password" => "test", "submit" => ""));

How would this code could be transfered to a working code in a Linux machine? What part am I missing? Thank you very much,

Regards.

  • 写回答

2条回答 默认 最新

  • doucan1979 2014-10-06 22:02
    关注

    Code was indeed perfect. All was needed is to create the file and give it the proper permissions. Inspired by Marc B. Thank you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下: