当前位置:首页 > 技术文档 > 正文内容

用PHP发送POST请求

admin10年前 (2015-11-15)技术文档1644
/**
 * 发送post请求
 * @param string $url 请求地址
 * @param array $post_data post键值对数据
 * @return string
 */
function send_post($url, $post_data) {

  $postdata = http_build_query($post_data);
  $options = array(
    'http' => array(
      'method' => 'POST',
      'header' => 'Content-type:application/x-www-form-urlencoded',
      'content' => $postdata,
      'timeout' => 15 * 60 // 超时时间(单位:s)
    )
  );
  $context = stream_context_create($options);
  $result = file_get_contents($url, false, $context);

  return $result;
}

//使用方法
$post_data = array(
  'username' => 'stclair2201',
  'password' => 'handan'
);
send_post('http://www.qianyunlai.com', $post_data);




<?php
/**
 * Socket版本
 * 使用方法:
 * $post_string = "app=socket&version=beta";
 * request_by_socket('chajia8.com', '/restServer.php', $post_string);
 */
function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30) {
  $socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);
  if (!$socket) die("$errstr($errno)");
  fwrite($socket, "POST $remote_path HTTP/1.0");
  fwrite($socket, "User-Agent: Socket Example");
  fwrite($socket, "HOST: $remote_server");
  fwrite($socket, "Content-type: application/x-www-form-urlencoded");
  fwrite($socket, "Content-length: " . (strlen($post_string) + 8) . "");
  fwrite($socket, "Accept:*/*");
  fwrite($socket, "");
  fwrite($socket, "mypost=$post_string");
  fwrite($socket, "");
  $header = "";
  while ($str = trim(fgets($socket, 4096))) {
    $header .= $str;
  }

  $data = "";
  while (!feof($socket)) {
    $data .= fgets($socket, 4096);
  }

  return $data;
}
?>

<?php
/**
 * Curl版本
 * 使用方法:
 * $post_string = "app=request&version=beta";
 * request_by_curl('http://www.qianyunlai.com/restServer.php', $post_string);
 */
function request_by_curl($remote_server, $post_string) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $remote_server);
  curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_USERAGENT, "qianyunlai.com's CURL Example beta");
  $data = curl_exec($ch);
  curl_close($ch);

  return $data;
}
?>


分享到:

扫描二维码推送至手机访问。

版权声明:本文由云河空间发布,如需转载请注明出处。

本文链接:http://yuyunhe.cn/index.php/post/115.html

分享给朋友:

“用PHP发送POST请求” 的相关文章

安卓刷机教程

 安卓手机刷机教程,体验不一样的新系统。    一:刷机准备与介绍  首先简单介绍下刷机吧,所谓的刷机,不纯指刷新的系统,其中还包括刷Recovery(也叫CWM或者工程模式),基带(你可以理解为电脑驱动 类),ROM(系统哦),SPL(你...

网页背景全屏拉伸的css效果

background: url(“http://www.wyzu.cn/uploadfile/2013/0220/20130220112423140.jpg”) repeat fixed center top / cover #595959;...

php 获取页面内容

function get_contents($url){     if(function_exists('file_get_contents')){       &nbs...

php 获取客户端的ip、地理信息、浏览器信息、本地真实ip

<?php  // 作用取得客户端的ip、地理信息、浏览器http://blog.qita.in  class get_gust_info {        ////获得访客浏...

用php gettext库来开发多语言系统

用php gettext库来开发多语言系统

通常人们写程序时都是将文字写死在程序里的, 比如:echo "Hello World!";  ,假如要改成它国语言,写国际化程序,就要逐个打开进行修改,程序较短时还行,若程序有上万甚至更多,改起来就不是那么容易了。近来随着i18n的逐渐标 准化,我也来讲一讲在PHP中如...

phpcms v9更换模板的具体操作方法

phpcms v9更换模板的具体操作方法

这篇文章主要介绍了phpcms v9更换模板的具体操作方法,需要的朋友可以参考下分享一下Phpcms V9更换模板的具体操作方法先分享下大概的步骤:1、上传模版文件到服务器;2、在站点管理 里边【模板风格配置】选择新模板;3、设置不同模型对应模板;4、修改现有的栏目,匹配新模板;5、更新栏目缓存、系...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。