如何使用 PHP cURL 检索验证码并保存会话?

发布于 2024-08-26 13:22:24 字数 2131 浏览 4 评论 0原文

更新:已解决

大家好,我已经知道了,只需保存 cookie 到临时文件,然后重新提交表单 卷曲并设置饼干与以前 临时文件:)感谢大家的回复:)

这是我的工作代码

$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url_register);  
        curl_setopt($ch, CURLOPT_USERAGENT, $this->useragent);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie); 
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);    
        $out['result']  = curl_exec($ch); 
        $out['error']   = curl_error($ch);
        $out['info']    = curl_getinfo($ch);
        curl_close($ch);

,对于下一个curl,只需使用CURLOPT_COOKIEFILE,就像这样

/* fetch captcha url with existed cookie */
            $ch = curl_init($captcha_url);
            curl_setopt($ch, CURLOPT_USERAGENT, $this->useragent);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie); 
            curl_setopt($ch, CURLOPT_FILE, $fp); 
            $out2['result'] = curl_exec($ch);
            $out2['error']  = curl_error($ch);
            $out2['info']   = curl_getinfo($ch);
            curl_close($ch);

大家好,

我正在创建一些脚本来通过phpcurl提交内容。首先获取会话和验证码,用户必须将验证码提交到最终提交。

问题是我无法获得验证码,我尝试使用此代码和 preg_match 来获取图像标签并返回它

    $ch = curl_init();

 curl_setopt($ch, CURLOPT_URL,$url);  
 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2');
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_COOKIE, 1);
 curl_setopt($ch, CURLOPT_COOKIEJAR, "1");
 curl_setopt($ch, CURLOPT_COOKIEFILE, "1");
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);    
    $result = curl_exec($ch);  
    curl_close($ch);

但没有运气,我尝试提交的页面是 http://abadijayaiklan.co.cc/pasang-iklan/

我希望有人能帮助我:)

谢谢和问候

UPDATE: SOLVED

Hi all, i've got it, just save cookie
to temp file, and resubmit form with
curl and set cookies with previous
temp file :) thanks all for respond :)

This my working code

$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url_register);  
        curl_setopt($ch, CURLOPT_USERAGENT, $this->useragent);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie); 
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);    
        $out['result']  = curl_exec($ch); 
        $out['error']   = curl_error($ch);
        $out['info']    = curl_getinfo($ch);
        curl_close($ch);

And for next curl just use CURLOPT_COOKIEFILE like this

/* fetch captcha url with existed cookie */
            $ch = curl_init($captcha_url);
            curl_setopt($ch, CURLOPT_USERAGENT, $this->useragent);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie); 
            curl_setopt($ch, CURLOPT_FILE, $fp); 
            $out2['result'] = curl_exec($ch);
            $out2['error']  = curl_error($ch);
            $out2['info']   = curl_getinfo($ch);
            curl_close($ch);

Hi all,

i'm create some script to submit content via php curl. first fetch session and captcha, and user must submit captcha to final submit.

the problem is i can't get captcha, i've try with this code and preg_match to get image tag and return it

    $ch = curl_init();

 curl_setopt($ch, CURLOPT_URL,$url);  
 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2');
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_COOKIE, 1);
 curl_setopt($ch, CURLOPT_COOKIEJAR, "1");
 curl_setopt($ch, CURLOPT_COOKIEFILE, "1");
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);    
    $result = curl_exec($ch);  
    curl_close($ch);

But no luck, page i'm trying to submit is http://abadijayaiklan.co.cc/pasang-iklan/.

I hope someone can help me out :)

Thanks and regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖微凉心微凉 2024-09-02 13:22:24

curl_setopt 的 php 手册页来看,CURLOPT_COOKIEFILECURLOPT_COOKIEJAR 都应该指定一个文件名。您将它们设置为“1”(这可能是有效的,但这是您想要的吗?)

From the php manual page on curl_setopt, CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR should both specify a filename. You have them set to '1' (which may be valid, but is that what you intended?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文