简体   繁体   中英

http get request timeout

I'm trying to send http request via socket. I've written the follosing code:

$sock = fsockopen('ooooo.ru', 80, $errno, $errstr, 5);
fputs($sock, "GET / HTTP/1.1\r\n");
fputs($sock, "Host: http://ooooo.ru/\r\n");
fputs($sock, "Content-type: text/html\r\n");
echo fgets($sock);

But I've a timeout error. But I can see the ooooo.ru via browser correctly. Why it's occuring?

You have to send a blank line to indicate the end of your HTTP request header, and you should not send a content-type when you don't have a request body to specify the type of.

Replace fputs($sock, "Content-type: text/html\\r\\n"); with fputs($sock, "\\r\\n");

The host also needs to be a host name and not a URL.

Replace fputs($sock, "Host: http://ooooo.ru/\\r\\n"); with fputs($sock, "Host: ooooo.ru\\r\\n");

Better yet, stop trying to write HTTP yourself and use the cURL library .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM