php cURL bot重定向问题 函数Baglan($url){ $user_agent='Mozilla/5.0(Windows;U;Windows NT 5.1;tr;rv:1.9.0.6)Gecko/2009011913 Firefox/3.0.6'; $ct=curl_init(); curl_setopt($ct,CURLOPT_URL,$URL); curl_setopt($ct,CURLOPT_RETURNTRANSFER,true); curl_setopt($ct,CURLOPT_USERAGENT,$user_agent); curl_setopt($ct,CURLOPT_SSL_VERIFYHOST,false); curl_setopt($ct,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ct,CURLOPT_COOKIEFILE,“cookies.txt”); $cikti=curl_exec($ct); 卷曲关闭($ct); 返回str_replace(数组(“\n”、“\t”、“\r”、)、null、$cikti); } 包括('mysql.php'); mysql_查询(“设置名称‘utf8’”); $baglan=baglan(“http://www.example.net/"); echo$baglan//当我插入echo命令时,我看到这个重定向代码:document.cookie=“nekil=bb1aecfdbebe52cc1be231de5ba04a4c3d”;location.href=”http://www.example.net/"; /*预赛# (.*?) #“,$baglan,$videolar); 印刷品(美元)*/

php cURL bot重定向问题 函数Baglan($url){ $user_agent='Mozilla/5.0(Windows;U;Windows NT 5.1;tr;rv:1.9.0.6)Gecko/2009011913 Firefox/3.0.6'; $ct=curl_init(); curl_setopt($ct,CURLOPT_URL,$URL); curl_setopt($ct,CURLOPT_RETURNTRANSFER,true); curl_setopt($ct,CURLOPT_USERAGENT,$user_agent); curl_setopt($ct,CURLOPT_SSL_VERIFYHOST,false); curl_setopt($ct,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ct,CURLOPT_COOKIEFILE,“cookies.txt”); $cikti=curl_exec($ct); 卷曲关闭($ct); 返回str_replace(数组(“\n”、“\t”、“\r”、)、null、$cikti); } 包括('mysql.php'); mysql_查询(“设置名称‘utf8’”); $baglan=baglan(“http://www.example.net/"); echo$baglan//当我插入echo命令时,我看到这个重定向代码:document.cookie=“nekil=bb1aecfdbebe52cc1be231de5ba04a4c3d”;location.href=”http://www.example.net/"; /*预赛# (.*?) #“,$baglan,$videolar); 印刷品(美元)*/,php,curl,Php,Curl,请阅读代码端的描述(在上面)。如何获取站点的真实代码?您必须解析响应中的url和cookie值,然后对解析后的url执行第二次请求,cookie设置为parsed value。 因此,代码应该如下所示: function Baglan($url) { $user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; tr; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6'; $ct = curl_init

请阅读代码端的描述(在上面)。如何获取站点的真实代码?

您必须解析响应中的url和cookie值,然后对解析后的url执行第二次请求,cookie设置为parsed value。 因此,代码应该如下所示:

function Baglan($url) {
    $user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; tr; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6';
$ct = curl_init();
curl_setopt($ct, CURLOPT_URL, $url);
curl_setopt($ct, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ct, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ct, CURLOPT_SSL_VERIFYHOST, false);
   curl_setopt($ct, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ct, CURLOPT_COOKIEFILE, "cookies.txt");
$cikti = curl_exec($ct);
curl_close($ct);
return str_replace(array("\n", "\t", "\r",), null, $cikti);

}
include ('mysql.php');
mysql_query("SET NAMES 'utf8'");
$baglan = Baglan("http://www.example.net/");
echo $baglan; //when I plug echo command, I see this redirect code: <html><body><script>document.cookie="nekil=bb1aecfdbe52cc1be231de5ba04a4c3d";location.href="http://www.example.net/";</script></body></html> 

/*preg_match('#<div class="loop-content switchable-view grid-mini" data-view="grid-mini">
                <div class="nag cf">(.*?)</div>
            </div><!-- end .loop-content -->#', $baglan, $videolar);
                print_r($videolar); <!-- Its not see real codes becouse of redirect issue becouse of that when I plug to work this function i see only Array() at the page -->*/

您必须解析响应中的url和cookie值,然后在cookie设置为parsed value的情况下对解析后的url执行第二次请求。您可以作为代码编写吗?似乎您没有尝试。代码运行良好:这是一个regexp问题,与问题中描述的重定向问题无关。我的代码达到了您想要的站点真实代码。对不起:(我不知道问题是什么..我如何解决regexp问题?这是另一个问题(问题).我已经解决了您的问题,正如原始问题中所描述的。您必须首先承认我的回答是一个解决方案。然后您可以问另一个问题。我可以这样做。但我真的不知道新问题该说什么。我真的不知道问题是什么?我只知道这是一个regexp问题。我从您那里知道的。
function parseResponse($resp) {
    $rx = '/document.cookie="([^"]+)".*location.href="([^"]+)"/i';
    return $resp && preg_match($rx, $resp, $arr)? array($arr[1], $arr[2]) : false;
}

function Baglan($url) {
    $user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; tr; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6';
    $a_opts = array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_USERAGENT => $user_agent,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLINFO_HEADER_OUT => true,
    );
    $ct = curl_init();
    curl_setopt_array($ct, $a_opts);
    curl_setopt($ct, CURLOPT_URL, $url);
    $a_resp = parseResponse(curl_exec($ct));
    if (!$a_resp) return false;
    curl_setopt_array($ct, $a_opts);
    curl_setopt($ct, CURLOPT_URL, $a_resp[1]);
    curl_setopt($ct, CURLOPT_COOKIE, $a_resp[0]);
    $cikti = curl_exec($ct);
    curl_close($ct);
    return str_replace(array("\n", "\t", "\r",), null, $cikti);
}