欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php發(fā)送post請(qǐng)求的三種方法

 更新時(shí)間:2014年02月11日 09:16:22   投稿:zxhpj  
這篇文章主要介紹了php發(fā)送post請(qǐng)求的三種方法,分別使用curl、file_get_content、fsocket來(lái)實(shí)現(xiàn)post提交數(shù)據(jù),需要的朋友可以參考下

方法一:

/**
 * 發(fā)送post請(qǐng)求
 * @param string $url 請(qǐng)求地址
 * @param array $post_data post鍵值對(duì)數(shù)據(jù)
 * @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 // 超時(shí)時(shí)間(單位: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.dbjr.com.cn', $post_data);

方法二:Socket版本

<?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;
}
?>

方法三:Curl版本

<?php
/**
 * Curl版本
 * 使用方法:
 * $post_string = "app=request&version=beta";
 * request_by_curl('http://www.dbjr.com.cn/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, "jb51.net's CURL Example beta");
  $data = curl_exec($ch);
  curl_close($ch);

  return $data;
}
?>

下面是其他網(wǎng)友的方法:

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
    
    $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;
  }
}

大家可以根據(jù)需要選擇適合自己的即可。

相關(guān)文章

  • php設(shè)計(jì)模式之享元模式分析【星際爭(zhēng)霸游戲案例】

    php設(shè)計(jì)模式之享元模式分析【星際爭(zhēng)霸游戲案例】

    這篇文章主要介紹了php設(shè)計(jì)模式之享元模式,結(jié)合星際爭(zhēng)霸游戲案例形式分析了PHP享元模式的相關(guān)原理與使用技巧,需要的朋友可以參考下
    2020-03-03
  • PHP多進(jìn)程編程總結(jié)(推薦)

    PHP多進(jìn)程編程總結(jié)(推薦)

    下面小編就為大家?guī)?lái)一篇PHP多進(jìn)程編程總結(jié)(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-07-07
  • PHP實(shí)現(xiàn)一個(gè)多功能購(gòu)物網(wǎng)站的案例

    PHP實(shí)現(xiàn)一個(gè)多功能購(gòu)物網(wǎng)站的案例

    下面小編就為大家?guī)?lái)一篇PHP實(shí)現(xiàn)一個(gè)多功能購(gòu)物網(wǎng)站的案例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • Zend Framework教程之視圖組件Zend_View用法詳解

    Zend Framework教程之視圖組件Zend_View用法詳解

    這篇文章主要介紹了Zend Framework教程之視圖組件Zend_View用法,較為詳細(xì)的分析了試圖組件Zend_View原理并結(jié)合實(shí)例形式分析了Zend_View的使用技巧,需要的朋友可以參考下
    2016-03-03
  • PHP內(nèi)核探索之變量

    PHP內(nèi)核探索之變量

    php變量由變量名,類型,內(nèi)容等部分組成,本文給大家分享php內(nèi)核探索之變量,對(duì)php內(nèi)核探索相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12
  • PHP 數(shù)組基本操作方法詳解

    PHP 數(shù)組基本操作方法詳解

    下面小編就為大家?guī)?lái)一篇PHP 數(shù)組基本操作方法詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-06-06
  • Laravel框架Eloquent ORM簡(jiǎn)介、模型建立及查詢數(shù)據(jù)操作詳解

    Laravel框架Eloquent ORM簡(jiǎn)介、模型建立及查詢數(shù)據(jù)操作詳解

    這篇文章主要介紹了Laravel框架Eloquent ORM簡(jiǎn)介、模型建立及查詢數(shù)據(jù)操作,結(jié)合實(shí)例形式詳細(xì)分析了laravel框架Eloquent ORM基本概念、原理、模型建立及數(shù)據(jù)查詢等相關(guān)使用技巧,需要的朋友可以參考下
    2019-12-12
  • PHP實(shí)現(xiàn)用session來(lái)實(shí)現(xiàn)記錄用戶登陸信息

    PHP實(shí)現(xiàn)用session來(lái)實(shí)現(xiàn)記錄用戶登陸信息

    在本篇文章里我們給大家分享了關(guān)于PHP如何用session來(lái)實(shí)現(xiàn)記錄用戶登陸信息的知識(shí)點(diǎn),有興趣的朋友們參考下。
    2018-10-10
  • Windows下php+mysql5.7配置教程

    Windows下php+mysql5.7配置教程

    這篇文章主要為大家詳細(xì)介紹了Windows下php+mysql5.7配置教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • PHP樹(shù)-不需要遞歸的實(shí)現(xiàn)方法

    PHP樹(shù)-不需要遞歸的實(shí)現(xiàn)方法

    下面小編就為大家?guī)?lái)一篇PHP樹(shù)-不需要遞歸的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-06-06

最新評(píng)論