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 创建文件(文件夹)以及目录操作代码
Mar 04 PHP
iis下php mail函数的sendmail配置方法(官方推荐)
Apr 25 PHP
PHP把数字转成人民币大写的函数分享
Jun 30 PHP
Thinkphp的volist标签嵌套循环使用教程
Jul 08 PHP
php获取URL中带#号等特殊符号参数的解决方法
Sep 02 PHP
新浪SAE搭建PHP项目教程
Jan 28 PHP
php限制上传文件类型并保存上传文件的方法
Mar 13 PHP
yii2中添加验证码的实现方法
Jan 09 PHP
PHP数据库表操作的封装类及用法实例详解
Jul 12 PHP
ucenter中词语过滤原理分析
Jul 13 PHP
PHP使用数组实现矩阵数学运算的方法示例
May 29 PHP
在Laravel中使用GuzzleHttp调用第三方服务的API接口代码
Oct 15 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 数组的指针操作实现代码
2011/02/08 PHP
php preg_replace替换实例讲解
2013/11/04 PHP
php实现改变图片直接打开为下载的方法
2015/04/14 PHP
Javascript this指针
2009/07/30 Javascript
简单介绍JavaScript的变量和数据类型
2015/06/03 Javascript
jquery UI Datepicker时间控件冲突问题解决
2016/12/16 Javascript
javascript实现去除HTML标签的方法
2016/12/26 Javascript
Vue.js进阶知识点总结
2018/04/01 Javascript
微信小程序获取音频时长与实时获取播放进度问题
2018/08/28 Javascript
angular 服务的单例模式(依赖注入模式下)详解
2018/10/22 Javascript
详解如何解决vue开发请求数据跨域的问题(基于浏览器的配置解决)
2018/11/12 Javascript
jquery+php后台实现省市区联动功能示例
2019/05/23 jQuery
详解vuex之store源码简单解析
2019/06/13 Javascript
Node在Controller层进行数据校验的过程详解
2020/08/28 Javascript
[07:31]DOTA2卡尔工作室 英雄介绍主宰篇
2013/06/25 DOTA
[02:19]2014DOTA2国际邀请赛 专访820少年们一起去追梦吧
2014/07/14 DOTA
[05:00]第二届DOTA2亚洲邀请赛主赛事第三天比赛集锦.mp4
2017/04/04 DOTA
[03:37]2016完美“圣”典 风云人物:Mikasa专访
2016/12/07 DOTA
[48:32]2018DOTA2亚洲邀请赛 3.31 小组赛 A组 LGD vs VG
2018/04/01 DOTA
Python的网络编程库Gevent的安装及使用技巧
2016/06/24 Python
Python IDLE入门简介
2017/12/08 Python
详解使用PyInstaller将Pygame库编写的小游戏程序打包为exe文件
2019/08/23 Python
Python连接Hadoop数据中遇到的各种坑(汇总)
2020/04/14 Python
python matplotlib.pyplot.plot()参数用法
2020/04/14 Python
matplotlib 对坐标的控制,加图例注释的操作
2020/04/17 Python
Pretty Little Thing爱尔兰:时尚女性服饰
2017/03/27 全球购物
Linux上比较文件的命令都有哪些
2012/02/24 面试题
家庭教育先进个人事迹材料
2014/01/24 职场文书
幼儿园师德师风学习材料
2014/05/29 职场文书
本科应届生自荐信
2014/06/29 职场文书
基层党员群众路线教育实践活动个人对照检查材料思想汇报
2014/10/05 职场文书
党的群众路线教育实践活动个人对照检查材料(企业)
2014/11/05 职场文书
春季运动会开幕词
2015/01/28 职场文书
给病人的慰问信
2015/03/23 职场文书
redis实现的四种常见限流策略
2021/06/18 Redis
React-vscode使用jsx语法的问题及解决方法
2021/06/21 Javascript