views:

116

answers:

2

i am trying to grab data from here : http://mediaforest.biz/mobile/nowplaying.aspx in the page you select a station and post it then you get new page with data. but i cant grab it, i get the same page again.

i used this code:

<?php
header ('Content-type: text/html; charset=utf-8');

$url = "http://mediaforest.biz/mobile/nowplaying.aspx";
$referer = "";

// headers
$header[] = "Host: ".parse_url($url, PHP_URL_HOST);
$header[] = "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; he; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3";
$header[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$header[] = "Accept-Language: he,en-us;q=0.7,en;q=0.3";
$header[] = "Accept-Encoding: gzip,deflate";
$header[] = "Accept-Charset: windows-1255,utf-8;q=0.7,*;q=0.7";
$header[] = "Keep-Alive: 115";
$header[] = "Connection: keep-alive";

$cookie="cookie.txt";
$fp=fopen($cookie,"w+");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_REFERER,$referer);
curl_setopt($ch, CURLOPT_TIMEOUT, 900);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_COOKIEFILE,$cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie);
curl_setopt($ch, CURLOPT_VERBOSE, 0);

$content=curl_exec($ch);
echo $content;

    if(stristr($content,"__EVENTTARGET")){
        $array1=explode('__EVENTTARGET" value="',$content);
        $content1=$array1[1];
        $array2=explode('">
<input type="hidden" name="__EVENTARGUMENT"',$content1);
        $content2=$array2[0];
        $EVENTTARGET=urlencode($content2);
    }

    if(stristr($content,"__EVENTARGUMENT")){
        $array1=explode('__EVENTARGUMENT" value="',$content);
        $content1=$array1[1];
        $array2=explode('">
<script language',$content1);
        $content2=$array2[0];
        $EVENTARGUMENT=urlencode($content2);
    }

    if(stristr($content,"formNowPlaying")){
        $array1=explode('method="post" action="',$content);
        $content1=$array1[1];
        $array2=explode('">
<input type="hidden" name="__EVENTTARGET"',$content1);
        $content2=$array2[0];
        $nexturl=$content2;
    }

//echo $EVENTTARGET." ".$EVENTARGUMENT." ".$nexturl;

$url = "http://mediaforest.biz/mobile/".$nexturl;

$fields = "EVENTTARGET=".$EVENTTARGET."&__EVENTARGUMENT=".$EVENTARGUMENT."&MyChannels=0&ViewChannel_Button=Show"; 

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_REFERER,$referer);
curl_setopt($ch, CURLOPT_TIMEOUT, 900);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_COOKIEFILE,$cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie);
curl_setopt($ch, CURLOPT_VERBOSE, 1);

$content_stage2=curl_exec($ch);
echo $content_stage2;
?>
A: 

I don't have much experience with cURL, and I didn't look into it, but at first glance it appears that this:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);

needs to be this:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

Could be wrong though.

Rob
i already tried that. it is not working
Ben
A: 

If you're getting data from the first request, try closing afterwards (for each request).

$content=curl_exec($ch); 
curl_close($ch); 
echo $content;

and

$content_stage2=curl_exec($ch);
curl_close($ch);    
echo $content_stage2; 
Jamie
i did it and getting the same results, maybe its not matter.
Ben