PHP 发 UDP 包时出错

今天金泉给了一个问题,说线上的腾讯 SDK PHP 版本的跑起来报错,看了一下,调用的代码类似这样

<?php
require("inc/OpenApiV3.php");

// 应用基本信息
$appid = xxxxxx;
$appkey = 'xxxxxxx';

// OpenAPI的服务器IP
// 最新的API服务器地址请参考wiki文档: http://wiki.open.qq.com/wiki/API3.0%E6%96%87%E6%A1%A3
$server_name = '119.147.19.43';

// 用户的OpenID/OpenKey
$openid = 'xxxxxxx';
$openkey = 'xxxxxxxx';

// 所要访问的平台, pf的其他取值参考wiki文档: http://wiki.open.qq.com/wiki/API3.0%E6%96%87%E6%A1%A3
$pf = 'qzone';


$sdk = new OpenApiV3($appid, $appkey);
$sdk->setServerName($server_name);

$ret = get_user_info($sdk, $openid, $openkey, $pf);
print_r("===========================n");
print_r($ret);

/**
 * 获取好友资料
 *
 * @param object $sdk OpenApiV3 Object
 * @param string $openid openid
 * @param string $openkey openkey
 * @param string $pf 平台
 * @return array 好友资料数组
 */
function get_user_info(&$sdk, $openid, $openkey, $pf)
{
	$params = array(
		'openid' => $openid,
		'openkey' => $openkey,
		'pf' => $pf,
	);

	$script_name = '/v3/user/get_info';

	return $sdk->api($script_name, $params);
}

// end of script

跑起来之后,报错是

Warning: socket_sendto() [function.socket-sendto]: unable to write to socket [1]: Operation not permitted in /home/naniwco1/public_html/draw/inc/lib/SnsStat.php on line 40

找到那个 SnsStat 文件,是一个用于统计的类,代码是这样的

<?php

/**
 * 统计上报接口调用情况
 *
 * @version 3.0.2
 * @author open.qq.com
 * @copyright © 2011, Tencent Corporation. All rights reserved.
 * @ History:
 *               3.0.2 | sparkeli | 2012-03-06 15:33:04 | initialize statistic fuction which can report API's access time and number to background server

 */

class SnsStat
{
	/**
	 * 执行一个 统计上报
	 *
	 * @param string $stat_url 统计上报的URL
	 * @param float $start_time 统计开始时间
	 * @param array $params 统计参数数组
	 * @return
	 */
	static public function statReport($stat_url, $start_time, $params)
	{
		$end_time = self::getTime();
		$params['time'] = round($end_time - $start_time, 4);
		$params['timestamp'] = time();
		$params['collect_point'] = 'sdk-php-v3';
		$stat_str = json_encode($params);
		//发送上报信息
		$host_ip = gethostbyname($stat_url);
		if ($host_ip != $stat_url)
		{
			$sock = socket_create(AF_INET, SOCK_DGRAM, 0);
			if (false === $sock)
			{
				return;
			}
			socket_sendto($sock, $stat_str, strlen($stat_str), 0, $host_ip, 19888);
			socket_close($sock);
		}
	}

	static public function getTime()
	{
		list($usec, $sec) = explode(" ", microtime());
		return ((float)$usec + (float)$sec);
	}
}

// end of script

把代码迁移到本地环境跑,在已经开了 socket PHP 扩展的前提下,是很顺利的,没有报错,于是考虑环境问题,google 一阵搜,看到这里,http://www.bluehostforum.com/s…

I took a quick look. Your script is attempting to connect out on port 27025 which is blocked unless it has been whitelisted to be allowed through the firewall. Contact support and they’ll be happy to open the port for you.

On an unrelated note, there is NEVER a reason to provide world-writable permissions to a file or directory on Bluehost’s servers. All script execution is done by your user which means that a file need only have user permissions (7xx for execution and 6xx for writing). World-writable permissions simply open your account to compromise.

说是防火墙问题,有一定的可能,但是好像没有办法验证,于是只能注释掉了。。

2 thoughts on “PHP 发 UDP 包时出错

Leave a Reply

Your email address will not be published. Required fields are marked *