php cURL bot redirect issue

304
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 data-view="grid-mini">
        <div >(.*?)</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 -->*/

Please read the descriptons of the code side(up there). How can I reach to real codes of site?

891

Answer

Solution:

You have to parse url and cookie value from the response, then perform 2nd request to the parsed url with cookie set to parsed value. So the code should look like this:

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);
}

People are also looking for solutions to the problem: php - Jquery post method not working

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.