Вопрос

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).

Seems like no matter what I try CURLOPT_COOKIE causes the page to timeout:

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); 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?

edit (more info): curl_getinfo() shows 500 http_code error when it times out.

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.

I tried CURLOPT_COOKIEFILE/CURLOPT_COOKIEJAR like this, but it still didn't work:

$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: enter image description here

edit: more info When I check the IIS logs I can see a difference between the two requests. The first one is with php and the second, working one, is with 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? Could this be the problem?

Это было полезно?

Решение

As stated in the PHP Manual, sessions doesn't support concurrent writes.

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.

Hope it helps.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top