Does not support fsockopen, but supports communication between ucenter and modoer in the culr environment

Source: Internet
Author: User
When the website was launched, modoer and ucenter could not communicate. I had to spend almost two days. at first, I thought that my configuration was faulty. after the platform was transplanted, I could not communicate. I modified the configuration several times, if the problem is not successful, it is suspected that the code is incorrect, the file permission is incorrect, or the function is not supported. After troubleshooting, it is found that the L1 host of HiChina does not support fsockopen, in the file uc_client/client. A problem occurs in uc_fopen in php. the code here is as follows:

The code is as follows:


Function uc_fopen ($ url, $ limit = 0, $ post = '', $ cookie ='', $ bysocket = FALSE, $ ip = '', $ timeout = 15, $ block = TRUE ){
$ Return = '';
$ Matches = parse_url ($ url );
! Isset ($ matches ['host']) & $ matches ['host'] = '';
! Isset ($ matches ['path']) & $ matches ['path'] = '';
! Isset ($ matches ['query']) & $ matches ['query'] = '';
! Isset ($ matches ['port']) & $ matches ['port'] = '';
$ Host = $ matches ['host'];
$ Path = $ matches ['path']? $ Matches ['path']. ($ matches ['query']? '? '. $ Matches ['query']: ''):'/';
$ Port =! Empty ($ matches ['port'])? $ Matches ['port']: 80;
If ($ post ){
$ Out = "POST $ path HTTP/1.0 \ r \ n ";
$ Out. = "Accept: */* \ r \ n ";
// $ Out. = "Referer: $ boardurl \ r \ n ";
$ Out. = "Accept-Language: zh-cn \ r \ n ";
$ Out. = "Content-Type: application/x-www-form-urlencoded \ r \ n ";
$ Out. = "User-Agent: $ _ SERVER [HTTP_USER_AGENT] \ r \ n ";
$ Out. = "Host: $ host \ r \ n ";
$ Out. = 'content-Length: '. strlen ($ post). "\ r \ n ";
$ Out. = "Connection: Close \ r \ n ";
$ Out. = "Cache-Control: no-cache \ r \ n ";
$ Out. = "Cookie: $ cookie \ r \ n ";
$ Out. = $ post;
} Else {
$ Out = "GET $ path HTTP/1.0 \ r \ n ";
$ Out. = "Accept: */* \ r \ n ";
// $ Out. = "Referer: $ boardurl \ r \ n ";
$ Out. = "Accept-Language: zh-cn \ r \ n ";
$ Out. = "User-Agent: $ _ SERVER [HTTP_USER_AGENT] \ r \ n ";
$ Out. = "Host: $ host \ r \ n ";
$ Out. = "Connection: Close \ r \ n ";
$ Out. = "Cookie: $ cookie \ r \ n ";
}
$ Fp = @ fsockopen ($ ip? $ Ip: $ host), $ port, $ errno, $ errstr, $ timeout );
If (! $ Fp ){
Return ''; // note $ errstr: $ errno \ r \ n
} Else {
Stream_set_blocking ($ fp, $ block );
Stream_set_timeout ($ fp, $ timeout );
@ Fwrite ($ fp, $ out );
$ Status = stream_get_meta_data ($ fp );
If (! $ Status ['timed _ out']) {
While (! Feof ($ fp )){
If ($ header = @ fgets ($ fp) & ($ header = "\ r \ n" | $ header = "\ n ")){
Break;
}
}
$ Stop = false;
While (! Feof ($ fp )&&! $ Stop ){
$ Data = fread ($ fp, ($ limit = 0 ||$ limit> 8192? 8192: $ limit ));
$ Return. = $ data;
If ($ limit ){
$ Limit-= strlen ($ data );
$ Stop = $ limit <= 0;
}
}
}
@ Fclose ($ fp );
Return $ return;
}
}


The fsockopen function cannot be used, because it can only rely on other methods. thanks to the support for curl and file_get_contents, the uc_fopen function is modified using curl;

The code is as follows:


Function uc_fopen ($ url, $ limit = 0, $ post = '', $ cookie ='', $ bysocket = FALSE, $ ip = '', $ timeout = 15, $ block = TRUE ){
$ Return = '';
$ Curl = curl_init ();
Curl_setopt ($ curl, CURLOPT_URL, $ url );
Curl_setopt ($ curl, CURLOPT_USERAGENT, $ _ SERVER ['http _ USER_AGENT ']);
If ($ post ){
Curl_setopt ($ curl, CURLOPT_POST, 1 );
Curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ post );
}
If ($ cookie ){
Curl_setopt ($ curl, CURLOPT_COOKIE, $ cookie );
}
Curl_setopt ($ curl, CURLOPT_TIMEOUT, $ timeout );
Curl_setopt ($ curl, CURLOPT_HEADER, 0 );
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 );
$ Return = curl_exec ($ curl );
If (curl_errno ($ curl )){
Echo'

Error:
'. Curl_error ($ curl );
}
Curl_close ($ curl );
Return $ return;
}


Therefore, uc_client/client under modoer. uc_cilent/client under php and uchome. php modified the uc_open function in this way. The first time curl is used, there are still a lot of information on the Internet, so there is no obstacle, but I don't know if this modification will affect other things, to be tested ....

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.