php使用socket post数据到其它web服务器的方法


Posted in PHP onJune 02, 2015

本文实例讲述了php使用socket post数据到其它web服务器的方法。分享给大家供大家参考。具体实现方法如下:

function post_request($url, $data, $referer='') {
  // Convert the data array into URL Parameters like a=b&foo=bar etc.
  $data = http_build_query($data);
  // parse the given URL
  $url = parse_url($url);
  if ($url['scheme'] != 'http') { 
    die('Error: Only HTTP request are supported !');
  }
  // extract host and path:
  $host = $url['host'];
  $path = $url['path'];
  // open a socket connection on port 80 - timeout: 30 sec
  $fp = fsockopen($host, 80, $errno, $errstr, 30);
  if ($fp){
    // send the request headers:
    fputs($fp, "POST $path HTTP/1.1\r\n");
    fputs($fp, "Host: $host\r\n");
    if ($referer != '')
      fputs($fp, "Referer: $referer\r\n");
    fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
    fputs($fp, "Content-length: ". strlen($data) ."\r\n");
    fputs($fp, "Connection: close\r\n\r\n");
    fputs($fp, $data);
    $result = ''; 
    while(!feof($fp)) {
      // receive the results of the request
      $result .= fgets($fp, 128);
    }
  }
  else { 
    return array(
      'status' => 'err', 
      'error' => "$errstr ($errno)"
    );
  }
  // close the socket connection:
  fclose($fp);
  // split the result header from the content
  $result = explode("\r\n\r\n", $result, 2);
  $header = isset($result[0]) ? $result[0] : '';
  $content = isset($result[1]) ? $result[1] : '';
  // return as structured array:
  return array(
    'status' => 'ok',
    'header' => $header,
    'content' => $content
  );
}
//使用方法
// Submit those variables to the server
$post_data = array(
  'test' => 'foobar',
  'okay' => 'yes',
  'number' => 2
);
// Send a request to example.com 
$result = post_request('http://www.example.com/', $post_data);
if ($result['status'] == 'ok'){
  // Print headers 
  echo $result['header']; 
  echo '<hr />';
  // print the result of the whole request:
  echo $result['content'];
}
else {
  echo 'A error occured: ' . $result['error']; 
}

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
PHP默认安装产生系统漏洞
Oct 09 PHP
PHP 和 MySQL 基础教程(四)
Oct 09 PHP
如何将一个表单同时提交到两个地方处理
Oct 09 PHP
从网上搜到的phpwind 0day的代码
Dec 07 PHP
php桌面中心(二) 数据库写入
Mar 11 PHP
php防攻击代码升级版
Dec 29 PHP
利用PHP+JS实现搜索自动提示(实例)
Jun 09 PHP
php调整gif动画图片尺寸示例代码分享
Dec 05 PHP
免费的ip数据库淘宝IP地址库简介和PHP调用实例
Apr 08 PHP
基于PHP实现商品成交时发送短信功能
May 11 PHP
Yii框架连接mongodb数据库的代码
Jul 27 PHP
[原创]PHP正则匹配中英文、数字及下划线的方法【用户名验证】
Aug 01 PHP
php自动给网址加上链接的方法
Jun 02 #PHP
php将字符串随机分割成不同长度数组的方法
Jun 01 #PHP
php正则preg_replace_callback函数用法实例
Jun 01 #PHP
php实现读取和写入tab分割的文件
Jun 01 #PHP
php从文件夹随机读取文件的方法
Jun 01 #PHP
php计算整个目录大小的方法
Jun 01 #PHP
php获取系统变量方法小结
May 29 #PHP
You might like
php匹配字符中链接地址的方法
2014/12/22 PHP
PHP SPL标准库之SplFixedArray使用实例
2015/05/12 PHP
php加密之discuz内容经典加密方式实例详解
2017/02/04 PHP
PHP中OpenSSL加密问题整理
2017/12/14 PHP
Laravel框架定时任务2种实现方式示例
2018/12/08 PHP
PHP 实现文件压缩解压操作的方法
2019/06/14 PHP
解决jquery的.animate()函数在IE6下的问题
2010/12/03 Javascript
javascript中onmouse事件在div中失效问题的解决方法
2012/01/09 Javascript
jquery实现隐藏与显示动画效果/输入框字符动态递减/导航按钮切换
2013/07/01 Javascript
JS Jquery 遍历,筛选页面元素 自动完成(实现代码)
2013/07/08 Javascript
js数组操作学习总结
2013/11/04 Javascript
上传图片js判断图片尺寸和格式兼容IE
2014/09/01 Javascript
PhotoShop给图片自动添加边框及EXIF信息的JS脚本
2015/02/15 Javascript
JavaScript之数组(Array)详解
2015/04/01 Javascript
js clearInterval()方法的定义和用法
2015/11/11 Javascript
javascript设置页面背景色及背景图片的方法
2015/12/29 Javascript
jQuery简单实现提交数据出现loading进度条的方法
2016/03/29 Javascript
jQuery实现当拉动滚动条到底部加载数据的方法分析
2019/01/24 jQuery
Vue-cli项目部署到Nginx服务器的方法
2019/11/01 Javascript
python中 logging的使用详解
2017/10/25 Python
Python实现通过继承覆盖方法示例
2018/07/02 Python
Django中使用Celery的教程详解
2018/08/24 Python
python3.8 微信发送服务器监控报警消息代码实现
2019/11/05 Python
Django 自动生成api接口文档教程
2019/11/19 Python
html5 offlline 缓存使用示例
2013/06/24 HTML / CSS
四议两公开实施方案
2014/03/28 职场文书
我读书我快乐演讲稿
2014/05/07 职场文书
争当四好少年演讲稿
2014/09/13 职场文书
2014购房个人委托书范本
2014/10/12 职场文书
体育个人工作总结
2015/02/09 职场文书
2015重阳节敬老活动总结
2015/07/29 职场文书
2015团员个人年度总结
2015/11/24 职场文书
幼儿园体操比赛口号
2015/12/25 职场文书
超详细Python解释器新手安装教程
2021/05/10 Python
python用海龟绘图写贪吃蛇游戏
2021/06/18 Python
排查并解决MySQL生产库内存使用率高的报警
2022/04/11 MySQL