0

I try to take some information from a site lets call it x site (http://212.237.41.34/player.php?ch=b3) When i try to connect that site from my pc it redirect me to http://www.cndhlsstream.pw/ site.


This is my problem; when i try to get content of x site with curl or file_get_contents it returns me only "invalid connection".

How can i solve this problem ?

I see on the bottom of x site there is a script

<script>
            if (top.location == self.location) {
                top.location = "http://www.cndhlsstream.pw/";
            }
 </script>

I think this make this error but i cannot make any solution.


I try this but it still return invalid connection

$context = stream_context_create(
        array(
            'http' => array(
                'follow_location' => false
            )
        )
    );

    echo file_get_contents('http://212.237.41.34/player.php?ch=b3',false,$context);
Will B.
  • 17,883
  • 4
  • 67
  • 69
Ahmet Karabulut
  • 153
  • 1
  • 12
  • Use [DomDocument](http://php.net/manual/en/class.domdocument.php) or `str_replace` to remove the script before rendering it. Since cURL and file_get_contents is not able to process the script elements. – Will B. Aug 04 '17 at 15:51
  • Show your cURL script please. – Will B. Aug 04 '17 at 16:02
  • function Baglan ($url) { $ch = curl_init (); curl_setopt_array ( $ch, array ( CURLOPT_URL => $url, CURLOPT_HTTPHEADER => array ( 'Connection: keep-alive' ), CURLOPT_USERAGENT => 'php/' . PHP_VERSION . ', libcurl/' . curl_version () ['version'], CURLOPT_ENCODING => '', CURLOPT_VERBOSE => true ) ); echo $ch; $cikti = curl_exec ( $ch ); curl_close( $ch ); return str_replace(array("\n","\t","\r"), null,$cikti); } – Ahmet Karabulut Aug 04 '17 at 16:46

3 Answers3

2

by default, neither curl nor file_get_contents send an user-agent header, nor do they send Connection: keep-alive. this website of yours is weird, it requires an user agent, AND it requires the header Connection: keep-alive, else it will respond with "invalid connect", so send both those headers to avoid the error. here's an (working) example with curl:

$ch = curl_init ();
curl_setopt_array ( $ch, array (
        CURLOPT_URL => 'http://212.237.41.34/player.php?ch=b3',
        CURLOPT_HTTPHEADER => array (
                'Connection: keep-alive'  // the server will just say "invalid connect" unless this header is sent, no idea why
        ),
        CURLOPT_USERAGENT => 'php/' . PHP_VERSION . ', libcurl/' . curl_version () ['version'], // without a useragent, the server will also say "invalid connect"
        CURLOPT_ENCODING => '', // <<the server supports gzip, this will make the transfer compressed, making it much faster
        CURLOPT_VERBOSE => true  // <<makes curl print lots of useful debugging info
) );
curl_exec ( $ch );
curl_close( $ch );

also note that curl is faster than file_get_contents, primarily for 2 reasons, 1: curl stops reading on content-length bytes, file_get_contents ignore this header, and just keeps reading until the connection is closed, which can be much slower. 2: curl supports compressed transfers (gzip & deflate), which file_get_contents does not support, and html compresses very, very well (easily 3-5 times smaller than its uncompressed counterparts in some tests i ran way back), and curl_ code works regardless of php.ini options, whereas file_get_contents rely on the allow_url_fopen php.ini option being enabled.

hanshenrik
  • 19,904
  • 4
  • 43
  • 89
  • Thanks for your awesome answer! It works but this time it redirect me to http://www.cndhlsstream.pw/ when curl_exec run, is there a way i stop it ? – Ahmet Karabulut Aug 04 '17 at 16:31
  • @AhmetKarabulut Add `CURLOPT_RETURNTRANSFER => true` to your `curl_setop_array` like so: `curl_set_opt_array($ch, array( ... CURLOPT_RETURNTRANSFER => true,));` and then `curl_exec` will return the data instead of output it. You can then assign a variable to store the data `$output = curl_exec($ch);` See: http://php.net/manual/en/function.curl-setopt.php – Will B. Aug 04 '17 at 17:27
  • @AhmetKarabulut yeah that's a javascript redirect for some reason,. you can add CURLOPT_RETURNTRANSFER, and strip out the redirect code like this ```$response = curl_exec ( $ch ); /* remove redirect */ $response = str_replace ( 'top.location = "http://www.cndhlsstream.pw/";', ';', $response ); echo $response;``` – hanshenrik Aug 04 '17 at 17:33
0

You need to specify a user agent for the external web server to accept your request.

$context = stream_context_create([
    'http' => [
        'user_agent' => 'any'
    ]
]);

$output = file_get_contents('http://212.237.41.34/player.php?ch=b3', false, $context);

To prevent the redirect use str_replace to extract the command.

$output = str_replace('top.location = "http://www.cndhlsstream.pw/";', '', $output);
var_dump($output);
Will B.
  • 17,883
  • 4
  • 67
  • 69
  • @AhmetKarabulut did you receive the same `invalid connection` error? As I only receive the error without the `'user_agent' => 'any'` flag being supplied. It should be identical to the cURL answer, except you can also use the `str_replace` to remove the Javascript redirect in either answer. – Will B. Aug 04 '17 at 17:21
-1
$context = stream_context_create(
    array(
        'http' => array(
            'follow_location' => false   //dont not follow redirect 
        )
    )
);

$html = file_get_contents('http://www.example.com/', false, $context);

var_dump($http_response_header);

refer : How do I ignore a moved-header with file_get_contents in PHP?

Arun pandian M
  • 862
  • 10
  • 17