Page 1 of 1

Bad Request with fsockopen?

Posted: Fri May 30, 2008 6:16 pm
by LuigiMario
Hi, I'm trying to use fopen to open and output a URL the code is below. This works fine, except when I try to use an https URL, it returns a "Bad Request" error. Can somebody tell me what I should do instead?

Code: Select all

 
$link = "http://www.google.com";
 
$http_response = "";
$url = parse_url($link);
$fp = fsockopen($url[host], 80, $err_num, $err_msg, 30) or die("Socket-open failed--error: ".$err_num." ".$err_msg);
fputs($fp, "GET /$url[path]?$url[query] HTTP/1.0\n");
fputs($fp, "Connection: close\n\n");
while(!feof($fp)) {
$http_response .= fgets($fp, 128);
}
fclose($fp);
echo $http_response;

Re: Bad Request with fsockopen?

Posted: Sat May 31, 2008 5:41 pm
by Weirdan
LuigiMario wrote:Hi, I'm trying to use fopen to open and output a URL the code is below. This works fine, except when I try to use an https URL, it returns a "Bad Request" error. Can somebody tell me what I should do instead?
Use a networking library with support for https (like curl). Or use ssl:// prefix to hostname when opening connection via fsockopen (might not work though, depends on php compile flags). You will need to change port number to 443 too, as it's the port the https protocol is mapped by default.