PHP:登录并导航到另一个页面

时间:2016-03-15 20:55:22

标签: php curl login

我确实需要使用curl登录页面,然后从该登录会话中,我必须导航到另一个页面来获取内容。下面是我使用的代码。但由于某种原因,它似乎并没有起作用。有人可以帮我吗?

<?php
//extract data from the post
//set POST variables

$url = 'https://test.curlrul.com/scripts/samplecenter.dll';

$fields = array(
    'cmd' => 'loginsubmit',
    'site' => 'myside',
    'retry' => '0',
    'acct_user_id' => 'myusername',
    'acct_password' => 'mypassword'
);
$fields_string = '';
//url-ify the data for the POST
foreach($fields as $key=>$value) { 
    $fields_string .= $key.'='.$value.'&'; 
}
rtrim($fields_string, '&');

//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name');  //could be empty, but cause problems on some hosts
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/mycookie');  //could be empty, but cause problems on some hosts
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);


//execute post
$result = curl_exec($ch);
if (curl_error($ch)) {
    echo curl_error($ch);
}


curl_setopt($ch, CURLOPT_URL, 'https://test.curlrul.com/scripts/samplecenter.dll?cmd=partsearch');
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");

$answer = curl_exec($ch);
if (curl_error($ch)) {
    echo curl_error($ch);
}
else {
    print_r($answer);
}

//close connection
curl_close($ch);

?>

0 个答案:

没有答案