Home >Backend Development >PHP Tutorial >Communication problem between ucenter and modoer in culr environment that does not support fsockopen but supports culr_PHP tutorial

Communication problem between ucenter and modoer in culr environment that does not support fsockopen but supports culr_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:25:13705browse

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 as follows:

Copy code 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.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;
}
}

The fsockopen function cannot be used, so you have to rely on other methods. Fortunately, curl is supported, and file_get_contents also supports it. I decided to use curl after considering it, and modified the uc_fopen function as follows;
Copy the code 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;
}

So uc_client/client.php under modoer and uc_cilent/client.php under uchome have modified the uc_open function in this way. 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 this. Whether the modification will affect other things remains to be tested. . . .

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324163.htmlTechArticleSo I suspected whether it was a coding problem, a file permission problem, or a function not supported problem. After investigation, I found out that it was Wanwang's L1 host does not support fsockopen, in the file uc_cl...
Statement:
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