1

嗨,我的卷曲有问题。我看了一篇旧帖子Remote Login not Working With Curl,但它不起作用。我听从了 baba 的建议,但代码在 if 语句中输入。对不起,我的英语不好。谁能帮我?

这是代码:

$url="http://hipfile.com/";
$urllog="http://hipfile.com/login.html"; 

$postdata = "login=bnnoor&password=########&op=login";

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt ($ch, 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 ($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_REFERER, $urllog); 

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt ($ch, CURLOPT_POST, 1); 
$result = curl_exec ($ch); 

if (!$result) { 
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    curl_close($ch); // make sure we closeany current curl sessions 
    die($http_code.' Unable to connect to server. Please come back later.'); 
} 
 echo $result;  
 curl_close($ch);
4

1 回答 1

0

你有饼干的初学者。似乎该站点在您登录时可能需要它们。下面的函数对我来说非常方便,verbose 选项将显示详细的 curl 错误,并且 'post' 选项可以设置为 post 字符串或值的关联数组,键是变量/字段名称及其值。我还注意到您在 hipfile 的表单中遗漏了一些 post 变量,这也可能是他们拒绝您登录的原因。

fetch('http://www.hipfile.com/');
$options['post'] = 'login=bnnoor&password=########&op=login';
$options['verbose'] = true;
$logged_in_page = fetch('http://www.hipfile.com/login.html',$options);

简单卷曲功能:

function fetch( $url, $z=null ) {
    $ch =  curl_init();
    $useragent = isset($z['useragent']) ? $z['useragent'] : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2';

    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $ch, CURLOPT_POST, isset($z['post']) );

    if( isset($z['post']) )         curl_setopt( $ch, CURLOPT_POSTFIELDS, $z['post'] );
    if( isset($z['refer']) )        curl_setopt( $ch, CURLOPT_REFERER, $z['refer'] );
    if( isset($z['ssl']) )          curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
    if( isset($z['ssl']) )          curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
    if( isset($z['verbose']) )      curl_setopt( $ch, CURLOPT_VERBOSE, true );
    if( isset($z['get_header']) )   curl_setopt( $ch, CURLOPT_HEADER, true );
    if( isset($z['header']) )       curl_setopt( $ch, CURLOPT_HTTPHEADER, $z['header'] );

    curl_setopt( $ch, CURLOPT_USERAGENT, $useragent );
    curl_setopt( $ch, CURLOPT_COOKIEJAR,  'cookiejarfilepath' );
    curl_setopt( $ch, CURLOPT_COOKIEFILE, 'cookiejarfilepath' );

    $result = curl_exec( $ch );
    curl_close( $ch );
    return $result;
}
于 2012-10-07T00:05:43.657 回答