rest_client.php
<?php
$a = array('a'=>1, 'b'=>2,'c'=>'I love you!','d'=>31343,'e'=>5);
$post = json_encode($a); //转化成Json
$limit = 8192;
$path = 'http://localhost/rest_api.php'; //服务端接口
$string = "POST $path HTTP/1.0/r/n";
$string .= "Accept: */*/r/n";
$string .= "Accept-Language: zh-cn/r/n";
$string .= "Content-Type: application/x-www-form-urlencoded/r/n";
$string .= "User-Agent: $_SERVER[HTTP_USER_AGENT]/r/n";
$string .= "Host: 127.0.0.1/r/n";
$string .= 'Content-Length: '.strlen($post)."/r/n";
$string .= "Connection: Close/r/n";
$string .= "Cache-Control: no-cache/r/n";
$string .= "Cookie: /r/n/r/n";
$string .= $post;
$fp = @fsockopen('127.0.0.1', 80, $errno, $errstr, 20);
stream_set_blocking($fp, TRUE);
stream_set_timeout($fp, 20);
@fwrite($fp, $string);
$status = stream_get_meta_data($fp);
if(!$status['timed_out']) {
while (!feof($fp)) {
if(($header = @fgets($fp)) && ($header == "/r/n" || $header == "/n")) {
break;
}
}
$stop = false;
$return = '';
while(!feof($fp) && !$stop) {
$data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
$return .= $data;
if($limit) {
$limit -= strlen($data);
$stop = $limit <= 0;
}
}
}
@fclose($fp);
echo $return;
?>
rest_api.php
<?php
// 服务端
echo $_SERVER['REQUEST_METHOD']; //方式获取
echo "<hr>";
$input = file_get_contents('php://input');
$input = json_decode($input);
$return = $input->a.$input->b.$input->c.$input->d.$input->e;
echo $return;
?>