Skip to content

Instantly share code, notes, and snippets.

@mgng
Last active February 8, 2021 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mgng/6182300 to your computer and use it in GitHub Desktop.
Save mgng/6182300 to your computer and use it in GitHub Desktop.
file_get_contents で Accept-Encoding:gzip 指定
<?php
function fetchUrl( $url, $gzip = false ) {
$raw = file_get_contents( $url, false, $context = stream_context_create( array(
'http' => array(
'method' => 'GET',
'header' => 'Accept-Encoding:' . ( $gzip ? 'gzip,deflate' : 'identity' ) . "\r\n",
)
) ) );
if ( $raw === false ) {
return false;
}
return $gzip ? gzdecode( $raw ) : $raw;
}
$src1 = fetchUrl( 'http://mgng.aws.af.cm' ); // 通常
$src2 = fetchUrl( 'http://mgng.aws.af.cm', true ); // gzip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment