douyanzan9145 2014-07-09 14:41
浏览 61

登录后cURL POST请求

I need to get a content from the page that requires authentication and after that need to create a post request for search criterias. I thought i need two cURL requests: first to login and save session data and second to submit a form... i tried several variants. The existing code can login and get the content but only this content that is before submiting the searchform. And also it can't use it session that i save and i the cookie.txt file is not empty. The second cURL requests says it needs the autentication again... My code:

    $host       = 'http://mydomain.ee/search/?&page=1';
    $username   = 'myusername';
    $password   = 'mypassword';
    $data       = array('tel' => 3, 'ok' => 'Otsi');

    $curlHandler = curl_init($host);
    curl_setopt($curlHandler, CURLOPT_HEADER, 1);
    curl_setopt($curlHandler, CURLOPT_USERPWD, $username . ":" . $password);
    curl_setopt($curlHandler, CURLOPT_TIMEOUT, 30);
    curl_setopt($curlHandler, CURLOPT_POST, 1);
    curl_setopt($curlHandler, CURLOPT_POSTFIELDS, "tel=3");
    curl_setopt($curlHandler, CURLOPT_POSTFIELDS, "ok=Otsi");
    curl_setopt($curlHandler, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curlHandler, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($curlHandler, CURL_VERBOSE, TRUE);
    curl_setopt($curlHandler, CURLOPT_COOKIESESSION, TRUE);
    curl_setopt($curlHandler, CURLOPT_COOKIEJAR, $this->files_path . 'cookie.txt');
    curl_setopt($curlHandler, CURLOPT_COOKIEFILE, $this->files_path . 'cookie.txt');

    $return = curl_exec($curlHandler);

    curl_close($curlHandler);
    print_r(htmlspecialchars($return));
    echo '<br/><br/>';

    $curlHandler = curl_init($host);
    curl_setopt($curlHandler, CURLOPT_HEADER, 1);
    //curl_setopt($curlHandler, CURLOPT_USERPWD, $username . ":" . $password);
    curl_setopt($curlHandler, CURLOPT_TIMEOUT, 30);
    curl_setopt($curlHandler, CURLOPT_POST, 2);
    //curl_setopt($curlHandler, CURLOPT_POSTFIELDS, "tel=3");
    //curl_setopt($curlHandler, CURLOPT_POSTFIELDS, "ok=Otsi");
    curl_setopt($curlHandler, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $post);
    curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curlHandler, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($curlHandler, CURL_VERBOSE, TRUE);
    curl_setopt($curlHandler, CURLOPT_COOKIESESSION, TRUE);
    //curl_setopt($curlHandler, CURLOPT_COOKIEJAR, $this->files_path . 'cookie.txt');
    curl_setopt($curlHandler, CURLOPT_COOKIEFILE, $this->files_path . 'cookie.txt');

    $return = curl_exec($curlHandler);

    curl_close($curlHandler);
    print_r(htmlspecialchars($return));

And like i said: i got html content after first curl_exec($curlHandler); but after second i get: HTTP/1.1 401 Authorization Required Date: Wed, 09 Jul 2014 14:19:20 GMT Server: Apache/2 WWW-Authenticate: Basic realm="Password" Content-Length: 18 Content-Type: text/html; charset=iso-8859-1 Unauthorized (401) Why it can't use it's cookie, filepath is the same and cooki.txt has it's content... how to fix that?

Thanks for any help!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)