功能强大的PHP POST提交数据类

yipeiwu_com5年前PHP代码库

本文实例为大家分享了PHP功能强大的 POST提交数据类,供大家参考,具体内容如下

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

  }

}

?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【宜配屋www.yipeiwu.com】。

相关文章

PHP 编写的 25个游戏脚本

无论是一个人玩简单的使用纸和笔的游戏,还是同一群人玩复杂的桌面角色扮演游戏,或者任意类型的联机游戏,本系列都提供了适合您的内容。“用 PHP 可以编写的 30 个游戏脚本” 系列中的每篇...

php获取数组元素中头一个数组元素值的实现方法

本文实例讲述了php获取数组元素中头一个数组元素值的实现方法。分享给大家供大家参考。具体如下: 在php的内置函数中,获取数组元素值的函数主要有 reset next current p...

PHP封装的MSSql操作类完整实例

本文实例讲述了PHP封装的MSSql操作类。分享给大家供大家参考,具体如下: <?php /*MSSql的操作类*/ class MSSql { var $link...

PHP使用mongoclient简单操作mongodb数据库示例

本文实例讲述了PHP使用mongoclient简单操作mongodb数据库。分享给大家供大家参考,具体如下: 最好回到《mongodb shell基础命令【进阶篇】》,再来看这里的内容,...

PHP详细彻底学习Smarty

基本语法  所有的smarty标签都被加上了定界符.在smarty里,所有定界符以外的内容都是静态的,当smarty遇到了模板标签,将尝试解释他们,然后再以恰当的方式输出.&n...