时时商务社区
标题:
php中用socket模拟http中post或者get提交数据的示例代码
[打印本页]
作者:
网络通达
时间:
2018-2-14 08:12
废话不多说。直接上代码:
sock_post.php:
[U]复制代码[/U] 代码如下:
$query = $data;
if(is_array($data)) $query = http_build_query($data);
$fp = @fsockopen($url['host'], $url['port'] ? $url['port'] : 80);
if (!$fp) return "Failed to open socket to $url[host]";
fputs($fp, sprintf("
OST %s%s%s HTTP/1.0/n", $url['path'], $url['query'] ? "?" : "", $url['query']));
fputs($fp, "Host: $url[host]/n");
fputs($fp, "Content-type: application/x-www-form-urlencoded/n");
fputs($fp, "Content-length: " . strlen($query) . "/n");
fputs($fp, "Connection: close/n/n");
fputs($fp, "$query/n");
$line = fgets($fp,1024);
if (@!eregi("^HTTP/1/.. 200", $line)) return;
$results = "";
$inheader = 1;
while(!feof($fp)) {
$line = fgets($fp,1024);
if ($inheader && ($line == "/n" || $line == "/r/n")) {
$inheader = 0;
}elseif (!$inheader) {
$results .= $line;
}
}
fclose($fp);
return $results;
}
$re = sock_post('http://localhost/direct_post/get_post.php?id=1',array('wel'=>'hello'));
echo $re;
?>
get_post.php:
[U]复制代码[/U] 代码如下:
您可能感兴趣的文章:
php socket方式提交的post详解
php中使用Curl、socket、file_get_contents三种方法POST提交数据
使用PHP Socket 编程模拟Http post和get请求
PHP中使用socket方式GET、POST数据实例
php使用socket post数据到其它web服务器的方法
php自定义类fsocket模拟post或get请求的方法
php 利用socket发送HTTP请求(GET,POST)
PHP socket 模拟POST 请求实例代码
欢迎光临 时时商务社区 (http://bbs.4435.cn/)
Powered by Discuz! X3.2