我是socket的初学者,哪个老大能指导我一下。使我能对socket编程有一定的了解。
參考答案:/*********************************************************
发送trackback程序
参数,主机地址,标题,这篇文章的地址,内容,站点名称
*********************************************************/
function sendTrackback($host,$title,$url,$excerpt,$blog_name) {
$buf="";
$host = str_replace('', '', $host);
$path = explode('/', $host);
$host = $path[0];
$host_array=explode(':', $host);
$host=$host_array[0];
if(empty($host_array[1])){
$port=80;
}else{
$port=$host_array[1];
}
unset($path[0]);
$path = '/' . implode('/', $path);
$excerpt = cn_substr($excerpt,252);
$fp = @fsockopen($host, $port, $errno, $errstr, 30);
if(!$fp) {
Return false;
}
$query = 'title=' . rawurlencode($title);
$query .= '&url=' . rawurlencode($url);
$query .= '&excerpt=' . rawurlencode($excerpt);
$query .= '&blog_name=' . rawurlencode($blog_name);
$out = 'POST ' . $path . ' HTTP/1.1' . "\r\n";
$out .= 'Host: ' . $host . "\r\n";
$out .= 'Connection: close' . "\r\n";
$out .= 'Content-Length: ' . strlen($query) . "\r\n";
$out .= 'Content-Type: application/x-www-form-urlencoded; charset=iso-8859-1' . "\r\n\r\n";
$out .= $query . "\r\n";
fwrite($fp, $out);
while (!feof($fp))
$buf .= fgets($fp,128);
fclose($fp);
$arr=explode("<error>",$buf);
$return_num=intval(substr($arr[1], 0, 1));
if($return_num==0){
return "发送成功 ";
}else{
$arr2=explode("message",$buf);
$return_msg=str_replace(">", "", $arr2[1]);
$return_msg=str_replace("</", "", $return_msg);
return "发送失败:".$return_msg;
}
}