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

yipeiwu_com6年前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笔记 字符串处理

substr(); substr(str,start,length); 例:substr('php is very good language',4,5); 输出为 is ve; 当st...

PHP.ini安全配置检测工具pcc简单介绍

PHP.ini安全配置检测工具pcc简单介绍

概述 前一段时间,在工作中遇到了一个开源程序,该程序主要用来检测PHP配置文件中得配置项是否存在安全隐患,并提出相应的配置建议,使PHP程序更加安全。 使用 这个程序使用起来非常简单,大...

PHP实现浏览器格式化显示XML的方法示例

PHP实现浏览器格式化显示XML的方法示例

本文实例讲述了PHP实现浏览器格式化显示XML的方法。分享给大家供大家参考,具体如下: 在头部加上 header("Content-type: application/xml");...

在PHP中利用wsdl创建标准webservice的实现代码

1、创建wsdl 说明: A、非标准的webservice,可能只能PHP才能访问 B、标准的webservice,就必须要使用wsdl(webservice description l...

PHP 程序员的调试技术小结

PHP 程序员的调试技术小结

本文介绍调试 PHP 应用程序的各种方法,包括在 Apache and PHP 中打开错误报告,以及通过在一个简单的 PHP 脚本中放置策略性的 print 语句,找到更困难的 bug...