Last active
December 10, 2015 01:35
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function http_file_get_contents( $url ) { | |
$response = file_get_contents( $url ); | |
return $response; | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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