Bad Request with fsockopen?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
LuigiMario
Forum Newbie
Posts: 1
Joined: Fri May 30, 2008 6:12 pm

Bad Request with fsockopen?

Post 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;
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Bad Request with fsockopen?

Post 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.
Post Reply