我正在努力能够根据需要自动登录到我的客户帐户并下载给定文档。作为尝试登录的第一步,我正在使用以下代码
<?php
$post_data=array();
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: "; // browsers keep this blank.
//create cURL connection
//this is just sample login url to have a session with the server
//this first access is to allow me get the hidden fields in the page
$curl_connection = curl_init('http://www.facebook.com');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,"Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($curl_connection, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_connection, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($curl_connection, CURLOPT_COOKIEFILE, "cookies.txt");
//perform our request
$result = curl_exec($curl_connection);
$result=htmlspecialchars($result);
/* str_replace("<","<",$result);
str_replace(">",">",$result); */
/* echo "<pre>";
print_r($result);
echo "</pre>"; */
$body_array=explode("<input",$result);
/* echo "<pre>";
print_r($body_array);
echo "</pre>"; */
foreach($body_array as $key =>$value)
{
$element_array1=explode('>',$value);
echo "<pre>";
// print_r($element_array1);
echo "</pre>";
if (strpos(strtolower($element_array1[0]), 'hidden') !== false) {
$we_have_hidden_array=explode('hidden',($element_array1[0]));
foreach($we_have_hidden_array as $hae)
{
$hae_wna= explode("name=",strstr(str_replace("'",'"',$hae),"name="));
$hae_wna=array_values(array_filter($hae_wna));
if(count($hae_wna) > 0)
{
$first_wna = explode(' ',$hae_wna[0]);
$final_keys=str_replace('"'," ",$first_wna[0]);
$final_key_array=explode(" ",$final_keys);
$final_key_array=array_values(array_filter($final_key_array));
$final_key=$final_key_array[0];
$post_data[$final_key]=0;
}
$hae_wna= explode("value=",strstr(str_replace("'",'"',$hae),"value="));
$hae_wna=array_values(array_filter($hae_wna));
if(count($hae_wna) > 0)
{
$first_wna = explode(' ',$hae_wna[0]);
$final_values=str_replace('"'," ",$first_wna[0]);
$final_value_array=explode(" ",$final_values);
$final_value_array=array_values(array_filter($final_value_array));
$final_value=$final_value_array[0];
$post_data[$final_key]=$final_value;
}
}
}
}
//create array of data to be posted
$post_data['username'] = 'admin';
$post_data['passwd'] = 'admin';
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
print_r($post_data);
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//create cURL connection
//this is just sample login
$curl_connection = curl_init('http://www.facebook.com');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,"Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl_connection, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_connection, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($curl_connection, CURLOPT_COOKIEFILE, "cookies.txt");
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
echo $result;
//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_getinfo($curl_connection, CURLINFO_HTTP_CODE);
echo curl_errno($curl_connection) . '-' .
curl_error($curl_connection);
//close the connection
curl_close($curl_connection);
?>
我的问题是,我无法找到工作的代码,获得状态200,那就是它......可能是我做错了什么?