开发者

command line cURL works but php one doesnt

开发者 https://www.devze.com 2023-01-14 09:10 出处:网络
The code: // That works pretty well curl -d \"user%5Blogin%5D=some%40email.pl&user%5Bpass%5D=testpass&user%5Bmemory%5D=on&user%5Bsubmit%5D=Login\" -L -c cookie.txt http://turbobit.net/use

The code:

// That works pretty well
curl -d "user%5Blogin%5D=some%40email.pl&user%5Bpass%5D=testpass&user%5Bmemory%5D=on&user%5Bsubmit%5D=Login" -L -c cookie.txt http://turbobit.net/user/login


//But this PHP code doesn't

$headers = array('Content-Type' => 'application/x-www-form-urlencoded', 'Referer' => 'http://turbobit.net/');
    $postdata = array('user%5Blogin%5D' => 'some%40email.pl', 'user%5Bpass%5D' => 'test', "user%5Bsubmit%5D" => 'Login', 'user%5Bmemory%5D' => 'on');
    $cookie = "/srv/http/test/regexturbobit/cookie.txt";

    $c = curl_init('ht开发者_运维百科tp://tutbobit.net/user/login');
    curl_setopt ($c, 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");
    curl_setopt($c, CURLOPT_POST, 1);
    curl_setopt ($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($c, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt ($c, CURLOPT_COOKIEJAR, $cookie);
    $output=curl_exec($c);
    curl_close($c);
    print_r($output);

it just doesn't show anything and even dont save cookie...


Try:

$postdata = 'user%5Blogin%5D=some%40email.pl&user%5Bpass%5D=testpass&user%5Bmemory%5D=on&user%5Bsubmit%5D=Login';

You shouldn't have URLencoded keys in a postdata array as they'll be URL-encoded again. Alternatively you could do:

$postdata = array(
  'user[login]' => 'some@email.pl',
  'user[pass]' =>  'test',
  // the rest of the vars here...
);

But note that passing an array for $postdata will not send a URL-encoded request, it will send a request with multipart/form-data encoding. From the PHP docs:

CURLOPT_POSTFIELDS

The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.

So, if you pass an array, the values and keys should not already be URLEncoded.


When you provide the data as a hash array, the POST will not be a "normal" POST anymore but it will instead make a multipart formpost.

Multipart formposts (RFC1867) are very different than the normal -d one your command line uses.

A nice trick is to use append "--libcurl example.c" to your curl command line to get to see the C source code for what could be used to run the same operation.


Try using this: http://github.com/shuber/curl

0

精彩评论

暂无评论...
验证码 换一张
取 消