Daci
Daci

Reputation: 27

Strange result from file_get_contents

<?php

    $content = file_get_contents('http://www.inc.com/patricia-fletcher/how-to-avoid-the-most-common-start-up-marketing-mistakes.html');
    var_dump($content);

?>

Why returns this?

strange result

If I replace the URL with http://google.com everything seems to be fine.

Upvotes: 0

Views: 93

Answers (2)

mhall
mhall

Reputation: 3701

The server is returning the content gzip:ed.

Screenshot from inspector

You therefore need to gunzip it to be able to read it. One way would be to use the Zlib functions:

$zd = gzopen('http://www.inc.com/patricia-fletcher/how-to-avoid-the-most-common-start-up-marketing-mistakes.html', "r");
$contents = gzread($zd, 100000);
gzclose($zd);

echo $contents;

Output:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>

    <link rel="icon" href="/favicon.ico" type="image/x-icon">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">

    <title>How to Avoid the Most Common Start-Up Marketing Mistakes | Inc.com</title>
<meta name="description" content="Know when and where to invest and get the most from your marketing dollars" />

...

Upvotes: 1

Danijel
Danijel

Reputation: 12689

Response is gzip compressed, use gzdecode().

$c = file_get_contents( 'http://www.inc.com/patricia-fletcher/how-to-avoid-the-most-common-start-up-marketing-mistakes.html' );
echo gzdecode($c);

Upvotes: 1

Related Questions