Skip to content

Instantly share code, notes, and snippets.

@leggetter
Last active December 10, 2015 01:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leggetter/4359352 to your computer and use it in GitHub Desktop.
Save leggetter/4359352 to your computer and use it in GitHub Desktop.
cURL is the defacto standard for making HTTP requests using PHP. But there are alternatives. Here are a few.
<?php
function http_file_get_contents( $url ) {
$response = file_get_contents( $url );
return $response;
}
?>
<?php
function http_fopen( $url ) {
$params = array(
'http' => array(
'method' => 'GET'
)
);
$ctx = stream_context_create($params);
$fp = @fopen( $url, 'rb', false, $ctx);
$response = '';
while( is_resource( $fp ) &&
$fp &&
!feof( $fp ) ) {
$response .= fread( $fp , 1024);
}
fclose($fp);
return $response;
}
?>
<?php
function http_fsockopen( $url )
{
$parts=parse_url($url);
$fp = fsockopen($parts['host'],
isset($parts['port'])?$parts['port']:80,
$errno, $errstr, 30);
$path = ( isset( $parts['path'] )? $parts['path'] : '/' );
$out = "GET ". $path ." HTTP/1.1\r\n";
$out.= "Host: ".$parts['host']."\r\n";
$out.= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
$response = '';
while( is_resource( $fp ) &&
$fp &&
!feof( $fp ) ) {
$response .= fread( $fp , 1024);
}
fclose($fp);
return $response;
}
?>
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once( 'http_fopen.php' );
require_once( 'http_fsockopen.php' );
require_once( 'http_file_get_contents.php' );
$url = 'http://www.leggetter.co.uk';
// $response = http_fopen( $url );
$response = http_fsockopen( $url );
//$response = http_file_get_contents( $url );
echo( $response );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment