Discuz教程网

不支持fsockopen但支持culr环境下下ucenter与modoer通讯问题

[复制链接]
authicon dly 发表于 2011-8-31 11:10:52 | 显示全部楼层 |阅读模式
所以就怀疑是否编码问题,或者文件权限问题,或者是不是函数不支持问题,经过排查发现原来是万网的L1主机不支持fsockopen,在文件uc_client/client.php中的uc_fopen中出现了问题,这里的代码是这样:
代码如下:

  1. function uc_fopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE) {
  2. $return = '';
  3. $matches = parse_url($url);
  4. !isset($matches['host']) && $matches['host'] = '';
  5. !isset($matches['path']) && $matches['path'] = '';
  6. !isset($matches['query']) && $matches['query'] = '';
  7. !isset($matches['port']) && $matches['port'] = '';
  8. $host = $matches['host'];
  9. $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
  10. $port = !empty($matches['port']) ? $matches['port'] : 80;
  11. if($post) {
  12. $out = "POST $path HTTP/1.0\r\n";
  13. $out .= "Accept: */*\r\n";
  14. //$out .= "Referer: $boardurl\r\n";
  15. $out .= "Accept-Language: zh-cn\r\n";
  16. $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
  17. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  18. $out .= "Host: $host\r\n";
  19. $out .= 'Content-Length: '.strlen($post)."\r\n";
  20. $out .= "Connection: Close\r\n";
  21. $out .= "Cache-Control: no-cache\r\n";
  22. $out .= "Cookie: $cookie\r\n\r\n";
  23. $out .= $post;
  24. } else {
  25. $out = "GET $path HTTP/1.0\r\n";
  26. $out .= "Accept: */*\r\n";
  27. //$out .= "Referer: $boardurl\r\n";
  28. $out .= "Accept-Language: zh-cn\r\n";
  29. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  30. $out .= "Host: $host\r\n";
  31. $out .= "Connection: Close\r\n";
  32. $out .= "Cookie: $cookie\r\n\r\n";
  33. }
  34. $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
  35. if(!$fp) {
  36. return '';//note $errstr : $errno \r\n
  37. } else {
  38. stream_set_blocking($fp, $block);
  39. stream_set_timeout($fp, $timeout);
  40. @fwrite($fp, $out);
  41. $status = stream_get_meta_data($fp);
  42. if(!$status['timed_out']) {
  43. while (!feof($fp)) {
  44. if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) {
  45. break;
  46. }
  47. }
  48. $stop = false;
  49. while(!feof($fp) && !$stop) {
  50. $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
  51. $return .= $data;
  52. if($limit) {
  53. $limit -= strlen($data);
  54. $stop = $limit <= 0;
  55. }
  56. }
  57. }
  58. @fclose($fp);
  59. return $return;
  60. }
  61. }
复制代码




fsockopen函数不能使用,因些就只能靠其它方法了,幸亏支持curl,file_get_contents也支持,经考虑就用curl吧,修改了uc_fopen函数,如下;
代码如下:

  1. function uc_fopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE) {
  2. $return = '';
  3. $curl = curl_init();
  4. curl_setopt($curl, CURLOPT_URL, $url);
  5. curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  6. if($post) {
  7. curl_setopt($curl, CURLOPT_POST, 1);
  8. curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
  9. }
  10. if($cookie) {
  11. curl_setopt($curl, CURLOPT_COOKIE, $cookie);
  12. }
  13. curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
  14. curl_setopt($curl, CURLOPT_HEADER, 0);
  15. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  16. $return = curl_exec($curl);
  17. if (curl_errno($curl)) {
  18. echo '<pre><b>错误:</b><br />'.curl_error($curl);
  19. }
  20. curl_close($curl);
  21. return $return;
  22. }
复制代码




于是modoer下的uc_client/client.php和uchome下的uc_cilent/client.php,就这样修改了uc_open函数,呵呵,第一次使用curl,网上的资料还是好多的,所以也没有什么阻碍,不过就不知这个修改会不会影响其它的东西,还有待测试罗。。。。




上一篇:PHP排序之二维数组的按照字母排序实现代码
下一篇:PHP中获取内网用户MAC地址(WINDOWS/linux)的实现代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 20:08

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表