本文分享下php发送post请求的三种方法与示例代码,分别使用curl、file_get_content、fsocket来实现post提交数据,大家做个参考。

  php发送post请求的三种方法,分别使用curl、file_get_content、fsocket来实现post提交数据。

  实例代码:

  代码示例:

  <?php

  //发送post请求数据

  class Request{

  public static function post($url, $post_data = '', $timeout = 5){//curl

  $ch = curl_init();

  curl_setopt ($ch, CURLOPT_URL, $url);

  curl_setopt ($ch, CURLOPT_POST, 1);

  if($post_data != ''){

  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

  }

  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

  curl_setopt($ch, CURLOPT_HEADER, false);

  $file_contents = curl_exec($ch);

  curl_close($ch);

  return $file_contents;

  }

  public static function post2($url, $data){//file_get_content

  // www.jbxue.com

  $postdata = http_build_query(

  $data

  );

  $opts = array('http' =>

  array(

  'method' => 'POST',

  'header' => 'Content-type: application/x-www-form-urlencoded',

  'content' => $postdata

  )

  );

  $context = stream_context_create($opts);

  $result = file_get_contents($url, false, $context);

  return $result;

  }

  public static function post3($host,$path,$query,$others=''){//fsocket

  $post="POST $path HTTP/1.1\r\nHost: $host\r\n";

  $post.="Content-type: application/x-www-form-";

  $post.="urlencoded\r\n${others}";

  $post.="User-Agent: Mozilla 4.0\r\nContent-length: ";

  $post.=strlen($query)。"\r\nConnection: close\r\n\r\n$query";

  $h=fsockopen($host,80);

  fwrite($h,$post);

  for($a=0,$r='';!$a;){

  $b=fread($h,8192);

  $r.=$b;

  $a=(($b=='')?1:0);

  }

  fclose($h);

  return $r;

  }

  } www.lefeng123.com

php发送post请求的三种方法示例的更多相关文章

  1. php发送post请求的三种方法

    引用:http://blog.sjzycxx.cn/post/435/ 1.使用 file_get_contents() /** * 发送post请求 * @param string $url 请求地 ...

  2. 【MySQL】锁——查看当前数据库锁请求的三种方法 20

    MySQL提供了查看当前数据库锁请求的三种方法:1. show  full  processlist命令  观察state和info列 2. show engine  innodb status\G ...

  3. 使用jmeter发送put请求的三种方式

    之前在前公司使用jmeter调试接口时,由于都是get和post请求,所以一直是顺风顺水的,毫无阻拦在短时间内调试完所有接口. 但是呢,在换到新公司后,发现接口请求是多式多样的,get.post必须有 ...

  4. vue中数据请求的三种方法

    注意请求可能存在跨域问题,需要去配置好 这三种建议使用axios 1.resource Vue 要实现异步加载需要使用到 vue-resource 库. Vue.js 2.0 版本推荐使用 axios ...

  5. Python使用requests发送post请求的三种方式

    1.我们使用postman进行接口测试的时候,发现POST请求方式的编码有3种,具体的编码方式如下: A:application/x-www-form-urlencoded ==最常见的post提交数 ...

  6. PHP发送HTTP请求的6种方法

    方法1: 用 file_get_contents 以get方式获取内容: <?php$url = 'https://wenda.shukaiming.com/';echo file_get_co ...

  7. PHP发送POST请求的三种方式

    class Request{ public static function post($url, $post_data = '', $timeout = 5){//curl $ch = curl_in ...

  8. php发送get、post请求的6种方法代码示例

    本文主要展示了php发送get.post请求的6种方法的代码示例,分别为使用file_get_contents .fopen.fsockopen.curl来发送GET和POST请求,代码如下: 方法1 ...

  9. SpringMVC的请求转发的三种方法

    SpringMVC请求转发的三种方法 首先明白请求转发是一次请求,地址栏不会发生变化,区别于重定向.springmvc环境自行配置. 以下举例中存在如下文件/WEB-INF/pages/success ...

随机推荐

  1. POJ2632 Crashing Robots(模拟)

    题目链接. 分析: 虽说是简单的模拟,却调试了很长时间. 调试这么长时间总结来的经验: 1.坐标系要和题目建的一样,要不就会有各种麻烦. 2.在向前移动过程中碰到其他的机器人也不行,这个题目说啦:a ...

  2. -_-#【Mac】命令

    切换cd $HOMEcd ~ 查看ls 查看系统显示:defaults write com.apple.finder AppleShowAllFiles -bool true隐藏:defaults w ...

  3. Silverlight 模板(Template)使用

    模板(Template)是控件另一种样式 它和样式(style)不同的是它允许已有的控件进行组合新的一个控件样式 那么先看一下最简单Template代码 xaml代码 <Button Conte ...

  4. C# 中dataTable中的数据批量增加至数据表中

    /// <summary> /// 批量添加 /// </summary> /// <param name="dt"></param> ...

  5. 解读sample2

    说明 理解被测试代码 理解测试代码 组织关系 检查字符串比较结果的断言 说明 被测试代码文件 sample2.h.sample2.cc 测试代码文件 sample2_unittest.cc 官网上如是 ...

  6. 获取机器本地的公网ip地址

    1. ipecho.net/plain

  7. adb devices: no permissions

    现象: #sudo adb root Error:insufficient permissions for device #adb devices List of devices attached ? ...

  8. NS CKD

    NS 定义:ALB<30:高脂血症:大量蛋白尿>3.5g:浮肿 见于:肾小球肾炎.小血管炎.微血管血栓性疾病.NSAIDs引起的急性间质性肾炎.不见于肾大血管病.间质性.小管性疾病. 并发 ...

  9. 一、进程与信号之exec函数system函数

    exec函数: 子进程调用exec函数执行另一个程序,exec函数进程完全由新程序代替,替换原有程序正文,数据,堆,栈段 #include <unistd.h> extern char * ...

  10. 数据迁移sql

    1.把数据库test中的表Table1中的数据插入到数据库test2中的表Table2:insert into test2.Table2(a,c,d) select a,c,5 from test.T ...