ucenter home does not support fsockopen but supports communication problems between ucenter and modoer in culr environment

WBOY
Release: 2016-07-29 08:46:31
Original
787 people have browsed it

So I suspected whether it was an encoding problem, a file permission problem, or a function not supported. After investigation, I found that Wanwang’s L1 host did not support fsockopen, and there was a problem in uc_fopen in the file uc_client/client.php. Here The code is like this:

Copy codeThe 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.0rn";
$out .= "Accept: */*rn";
//$out .= "Referer: $boardurlrn";
$out .= "Accept-Language: zh-cnrn";
$out .= "Content-Type: application/x-www-form-urlencodedrn";
$out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]rn";
$out .= "Host: $hostrn";
$out .= 'Content-Length: '.strlen($post)."rn" ;
$out .= "Connection: Closern";
$out .= "Cache-Control: no-cachern";
$out .= "Cookie: $cookiernrn";
$out .= $post;
} else {
$out = "GET $path HTTP/1.0rn";
$out .= "Accept: */*rn";
//$out .= "Referer: $boardurlrn";
$out .= "Accept -Language: zh-cnrn";
$out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]rn";
$out .= "Host: $hostrn";
$out .= "Connection: Closern";
$out .= "Cookie: $cookiernrn";
}
$fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
if(!$fp ) {
return '';//note $errstr : $errno rn
} 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 == "rn" || $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;
}
}


fsockopen function cannot be used, so only I can rely on other methods. Fortunately, curl is supported, and file_get_contents also supports it. After considering it, I will use curl. I modified the uc_fopen function as follows;

Copy the codeThe 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;
}

So uc_client/client.php and uhome under modoer uc_cilent/client.php under, just modified the uc_open function, haha, it is my first time to use curl, there is 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, or Still to be tested. . . .
The above introduces the communication problem between ucenter and modoer under ucenter home which does not support fsockopen but supports culr environment, including the content of ucenter home. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!