To solve this problem, php often fails to respond to requests from other servers.-php Tutorial

Source: Internet
Author: User
To solve this problem, php often fails to respond to requests from other servers. 1. use file_get_contents directly to implement file_get_contents ($ url); 2. use curl extension. & nbsp; $ chcurl_init (); & nbsp; $ timeout30; & nbsp; curl_setopt ($ ch, CURLO for obfuscation, php often fails to respond to requests from other servers.
1. use file_get_contents directly.
File_get_contents ($ url );

2. use curl extension.
$ Ch = curl_init ();
$ Timeout = 30;
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 );
$ Contents = trim (curl_exec ($ ch ));
Curl_close ($ ch );

3 socket.
$ Address = 'http: // 1.2.3.4: 9001 /';
$ Path = '/aaa/bbb ';
$ Sock = fsockopen ($ address, 80, $ errno, $ errstr, 30 );
If (! $ Sock) die ("$ errstr ($ errno) \ n ");
$ Psw = md5 ('192. 100 ');
$ Data = "xxx = xxx & yyy = yyy ";
Fwrite ($ sock, "POST $ path HTTP/1.0 \ r \ n ");
Fwrite ($ sock, "Host: localhost \ r \ n ");
Fwrite ($ sock, "Content-type: application/x-www-form-urlencoded \ r \ n ");
Fwrite ($ sock, "Content-length:". strlen ($ data). "\ r \ n ");
Fwrite ($ sock, "Accept: */* \ r \ n ");
Fwrite ($ sock, "\ r \ n ");
Fwrite ($ sock, "$ data \ r \ n ");
Fwrite ($ sock, "\ r \ n ");
$ Headers = "";
While ($ str = trim (fgets ($ sock, 4096 )))
$ Headers. = "$ str \ n ";
$ Body = "";
While (! Feof ($ sock ))
$ Body. = fgets ($ sock, 4096 );
Fclose ($ sock );
$ Body1 = explode ("#", $ body );
Return $ body1;

All three methods are slow.

Echo ""; output is normal.

The above code is actually run in the for loop. The effect of packet capture below

Previously, the access was quite good. recently, it often gets stuck during use. An error is reported.
Please help me!

------ Solution --------------------
1. check whether the cookie is affected.
2. set the route

They are all intranet addresses. what are they doing if they are so messy?
------ Solution --------------------
Is the source address network poor?
------ Solution --------------------
Discussion

It is obviously requested, and the connection resources are limited. Is there a way to establish a persistent connection. Use this resource when necessary. You do not need to open a resource every time you connect.

------ Solution --------------------
Discussion

It is obviously requested, and the connection resources are limited. Is there a way to establish a persistent connection. Use this resource when necessary. You do not need to open a resource every time you connect.

------ Solution --------------------
The connection is too frequent. suspected DOS attacks are blacklisted.

Directly use curl to request keepalive.

CURLOPT_FRESH_CONNECT => false
CURLOPT_FORBID_REUSE => false

Configure these two options. you do not need to disable curl_exec and continue to use it next time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.