php函数fsockopen设置,PHP中 fsockopen 函数怎么用?

在PHP中fsockopen函数的作用是打开一个网络连接或者一个Unix套接字连接,其语法为“fsockopen($hostname) ”,返回值为一个文件句柄,之后可以被其他文件类函数调用。

df7dc0e5ffad6ab1a0f03b46bb65a1c6.png

简单示例<?php

$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);

if (!$fp) {

echo "$errstr ($errno)
\n";

} else {

$out = "GET / HTTP/1.1\r\n";

$out .= "Host: www.example.com\r\n";

$out .= "Connection: Close\r\n\r\n";

fwrite($fp, $out);

while (!feof($fp)) {

echo fgets($fp, 128);

}

fclose($fp);

}

?>

使用UDP连接<?php

$fp = fsockopen("udp://127.0.0.1", 13, $errno, $errstr);

if (!$fp) {

echo "ERROR: $errno - $errstr
\n";

} else {

fwrite($fp, "\n");

echo fread($fp, 26);

fclose($fp);

}

?>

使用示例<?php

$host = "something.example.com";

$port = 443;

$path = "/the/url/path/file.php"; //or .dll, etc. for authnet, etc.

//you will need to setup an array of fields to post with

//then create the post string

$formdata = array ( "x_field" => "somevalue");

//build the post string

foreach($formdata AS $key => $val){

$poststring .= urlencode($key) . "=" . urlencode($val) . "&";

}

// strip off trailing ampersand

$poststring = substr($poststring, 0, -1);

$fp = fsockopen("ssl://".$host, $port, $errno, $errstr, $timeout = 30);

if(!$fp){

//error tell us

echo "$errstr ($errno)\n";

}else{

//send the server request

fputs($fp, "POST $path HTTP/1.1\r\n");

fputs($fp, "Host: $host\r\n");

fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");

fputs($fp, "Content-length: ".strlen($poststring)."\r\n");

fputs($fp, "Connection: close\r\n\r\n");

fputs($fp, $poststring . "\r\n\r\n");

//loop through the response from the server

while(!feof($fp)) {

echo fgets($fp, 4096);

}

//close fp - we are done with it

fclose($fp);

}推荐教程:《PHP》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值