Hom's Blog


PHP-POST-异步调用

php 通过fsocket模块 post 提交请求

<?php
function sock_post($url,$query){
	$info=parse_url($url);
	$fp=fsockopen($info["host"],80,$errno,$errstr,3);
	$head="POST ".$info['path']." HTTP/1.0\r\n";
	$head.="Host: ".$info['host']."\r\n";
	$head.="Referer: http://".$info['host'].$info['path']."\r\n";
	$head.="Content-type: application/x-www-form-urlencoded\r\n";
	$head.="Content-Length: ".strlen(trim($query))."\r\n";
	$head.="\r\n";
	$head.=trim($query);
	$write=fputs($fp,$head);
	while(!feof($fp)){
		$line=fgets($fp);
		echo $line."<br>";
	}
}
?>

php 通过 curl 模拟 post 提交请求

<?php
$url='http://www.phpernote.com/post.php';
$fields=array(
	'lname'=>'justcoding',
	'fname'=>'phplover',
	'title'=>'myapi',
	'email'=>'403656085@qq.com',
	'phone'=>'13611975347'
);
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
ob_start();
curl_exec($ch);
$result=ob_get_contents();
ob_end_clean();
echo $result;
curl_close($ch);
?>

Reference

  1. php 不等待返回的实现方法(异步调用)
  2. 使用fscok实现异步调用PHP
  3. 使用PHP进行异步HTTP请求


◆ 本文地址: http://platinhom.github.io/2015/07/03/PHP-asyn-comp/, 转载请注明 ◆

前一篇: PHP利用curl来Post表单和上传文件
后一篇: PHP服务器端不显示错误报告的重启解决办法


Contact: Hom / 已阅读()
Source 类别: Coding  标签: PHP  Web