简体   繁体   English

CURLOPT_COOKIE导致超时

[英]CURLOPT_COOKIE is cause a timeout

I'm trying to pass php curl the current logged in session (so I don't have to log in with curl before sending form data). 我正在尝试通过php curl当前登录的会话(所以我不必在发送表单数据之前使用curl登录)。

Seems like no matter what I try CURLOPT_COOKIE causes the page to timeout: 似乎无论我尝试什么CURLOPT_COOKIE导致页面超时:

function postForm($post_data, $URL) {
    //traverse array and prepare data for posting (key1=value1)
    foreach ( $post_data as $key => $value) {
        $post_items[] = $key . '=' . $value;
    }

    //create cURL connection
    $curl_connection = curl_init($URL);

    //set curl to current session
    //$strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';
    //$strCookie = session_name() . '=' . session_id());
    $strCookie = 'PHPSESSID=' . session_id() . '; path=/';
    $useragent = $_SERVER['HTTP_USER_AGENT'];

    //set curl options
    curl_setopt($curl_connection, CURLOPT_USERAGENT,$useragent);
    curl_setopt($curl_connection, CURLOPT_COOKIE,$strCookie); 
    curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
        ...


//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

Line curl_setopt($curl_connection, CURLOPT_COOKIE,$strCookie); line curl_setopt($curl_connection, CURLOPT_COOKIE,$strCookie); is causing the script execution to timeout. 导致脚本执行超时。

How can I pass cUrl the current session so it does not need to login to post a form? 如何将cUrl传递给当前会话,以便它不需要登录即可发布表单?

edit (more info): curl_getinfo() shows 500 http_code error when it times out. 编辑(更多信息):curl_getinfo()在超时时显示500 http_code错误。

edit: 编辑:

Well I still can't get this to work. 好吧,我仍然无法让这个工作。 I can send the form fine in the POSTMAN REST Client Chrome extension, but I can't figure out how to do this with curl. 我可以在POSTMAN REST Client Chrome扩展程序中发送表单,但我无法弄清楚如何使用curl执行此操作。

I tried CURLOPT_COOKIEFILE/CURLOPT_COOKIEJAR like this, but it still didn't work: 我这样尝试了CURLOPT_COOKIEFILE / CURLOPT_COOKIEJAR,但它仍然无效:

$useragent = $_SERVER['HTTP_USER_AGENT'];
$tmp_fname = tempnam("/tmp", "COOKIE");

//set curl options
...
curl_setopt($curl_connection, CURLOPT_COOKIEJAR, $tmp_fname);
curl_setopt($curl_connection, CURLOPT_COOKIEFILE, $tmp_fname);
...

If it helps here is how I sent it with POSTMON, seems pretty straight forward, don't know why I can't get it working with curl: 如果它有帮助我是如何用POSTMON发送的,看起来非常直接,不知道为什么我不能使用curl: 在此输入图像描述

edit: more info When I check the IIS logs I can see a difference between the two requests. 编辑:更多信息当我检查IIS日志时,我可以看到两个请求之间的差异。 The first one is with php and the second, working one, is with POSTMON: 第一个是php,第二个是工作,是POSTMON:

2014-04-17 22:31:44 ::1 POST /mysite/categories.php ccsForm=categories_maint 80 - ::1 Mozilla/5.0+(Windows+NT+6.1)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/33.0.1750.154+Safari/537.36 200 0 64 109717
2014-04-17 22:34:21 SERVERS.IP.ADDRESS.HERE POST /mysite/categories.php ccsForm=categories_maint 80 - MY.IP.ADDRESS.HERE Mozilla/5.0+(Windows+NT+6.1)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/33.0.1750.154+Safari/537.36 302 0 0 62

It looks like with PHP its trying to send the request from itself? 它似乎与PHP试图从自己发送请求? Could this be the problem? 这可能是问题吗?

As stated in the PHP Manual , sessions doesn't support concurrent writes. PHP手册中所述,会话不支持并发写入。

but as session data is locked to prevent concurrent writes only one script may operate on a session at any time. 但是,由于会话数据被锁定以防止并发写入,因此任何时候只有一个脚本可以在会话上运行。

Then the timeout issue cause is that the main script accessed your session first, so while it's still running, the session will remain inaccessible to other scripts. 然后超时问题的原因是主脚本首先访问了您的会话,因此当它仍在运行时,其他脚本仍然无法访问该会话。

You can use the session_write_close for this purpose before running curl_exec to make your session data accessible to the cURL call. 在运行curl_exec之前,可以将session_write_close用于此目的,以使cURL调用可以访问会话数据。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM