PHP发送POST请求的三种方式

正文开始

 public static function post($url, $post_data = '', $timeout = 5){//curl<br /> <br />        $ch = curl_init();<br /> <br />        curl_setopt ($ch, CURLOPT_URL, $url);<br /> <br />        curl_setopt ($ch, CURLOPT_POST, 1);<br /> <br />        if($post_data != ''){<br /> <br />            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);<br /> <br />        }<br /> <br />        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); <br /> <br />        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);<br /> <br />        curl_setopt($ch, CURLOPT_HEADER, false);<br /> <br />        $file_contents = curl_exec($ch);<br /> <br />        curl_close($ch);<br /> <br />        return $file_contents;<br /> <br />    }<br /> <br /> <br />    public static function post2($url, $data){//file_get_content<br /> <br />         <br /> <br />        $postdata = http_build_query(<br /> <br />            $data<br /> <br />        );<br /> <br />         <br /> <br />        $opts = array('http' =><br /> <br />                      array(<br /> <br />                          'method'  => 'POST',<br /> <br />                          'header'  => 'Content-type: application/x-www-form-urlencoded',<br /> <br />                          'content' => $postdata<br /> <br />                      )<br /> <br />        );<br /> <br />         <br /> <br />        $context = stream_context_create($opts);<br /> <br /> <br />        $result = file_get_contents($url, false, $context);<br /> <br />        return $result;<br /> <br /> <br />    }<br /> <br /> <br />    public static function post3($host,$path,$query,$others=''){//fsocket<br /> <br /> <br />        $post="POST $path HTTP/1.1rnHost: $hostrn";<br /> <br />        $post.="Content-type: application/x-www-form-";<br /> <br />        $post.="urlencodedrn${others}";<br /> <br />        $post.="User-Agent: Mozilla 4.0rnContent-length: ";<br /> <br />        $post.=strlen($query)."rnConnection: closernrn$query";<br /> <br />        $h=fsockopen($host,80);<br /> <br />        fwrite($h,$post);<br /> <br />        for($a=0,$r='';!$a;){<br /> <br />                $b=fread($h,8192);<br /> <br />                $r.=$b;<br /> <br />                $a=(($b=='')?1:0);<br /> <br />            }<br /> <br />        fclose($h);<br /> <br />        return $r;<br /> <br />    }

正文结束

PHP接口(interface)和抽象类(abstract) PHP的耦合设计模式