php发送post方式二

1668277006775

方法二:Socket版本

<?php

/**

* Socket版本

* 使用方法:

* $post_string = “app=socket&version=beta”;

* request_by_socket(‘chajia8.com’, ‘/restServer.php’, $post_string);

*/

function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30) {

$socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);

if (!$socket) die(“$errstr($errno)”);

fwrite($socket, “POST $remote_path HTTP/1.0”);

fwrite($socket, “User-Agent: Socket Example”);

fwrite($socket, “HOST: $remote_server”);

fwrite($socket, “Content-type: application/x-www-form-urlencoded”);

fwrite($socket, “Content-length: ” . (strlen($post_string) + 8) . “”);

fwrite($socket, “Accept:*/*”);

fwrite($socket, “”);

fwrite($socket, “mypost=$post_string”);

fwrite($socket, “”);

$header = “”;

while ($str = trim(fgets($socket, 4096))) {

$header .= $str;

}

 

$data = “”;

while (!feof($socket)) {

$data .= fgets($socket, 4096);

}

 

return $data;

}

?>

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容