一.curl
$url = 'http://localhost/http/1.php';
$postData = array(
'title'=>'我是curl',
'content'=>'我是curl的内容'
);
//1.初始化一个curl会话
$ch = curl_init();
//2.设置相应的会话选项
//设置提交的网址
curl_setopt($ch,CURLOPT_URL,$url);
//设置数据提交的方式
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postData);
//提交成功之后,把数据返回为字符串
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
//3.提交会话
$str=curl_exec($ch);
//4.关闭会话
curl_close($ch);
echo $str;
二.socket
fsockopen(主机,端口,错误号,错误信息,套接字);
fsockopen( string $hostname [, int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )
$postData = array(
'title'=>'我是socket方式提交的',
'content'=>'我是socket的内容'
);
$postData = http_build_query($postData);
$fp = fsockopen('localhost',80,$errno,$errorStr,5);
$request = "POST http://localhost/http/1.php HTTP/1.1"."\r\n";
$request .="Host:localhost\r\n";
$request .="Content-type:application/x-www-form-urlencoded\r\n";
$request .="Content-length:".strlen($postData)."\r\n\r\n";
$request .= $postData;
//var_dump($request);
fwrite($fp,$request);
//读取数据
while(!feof($fp)){
echo fgets($fp,1024);
}