php之post请求的方法

来源:互联网 发布:淘宝摄影棚所需要器材 编辑:程序博客网 时间:2024/05/29 02:27

方法一:

         /**   

 * 发送post请求   

 *@param string $url 请求地址   

 *@param array $data_ data post键值对数据   

 *@return string 

 */ 

当为Content-type: application/json格式时:

public function Post($url,$post_data){

       // $postdata = http_build_query($post_data);

        $opts = array('http' =>

        array(

 

                          'method'  => 'POST',

 

                          'header'  => 'Content-type: application/json',

 

                          'content' =>$post_data,

                         

                                                          'timeout' => 15 * 60 // 超时时间(单位:s)   

                      )

 

       );

       $context = stream_context_create($opts);

       $result = file_get_contents($url, false, $context);

 

       return $result;

 

 

    }

当为 application/x-www-form-urlencoded格式时:

public function Post($url,$post_data){

         $postdata =http_build_query($post_data);

        $opts = array('http' =>

        array(

 

                          'method'  => 'POST',

 

                          'header'  =>

                  'Content-type: application / application/x-www-form-urlencoded ',

 

                          'content' => $ postdata,

                         

                                                          'timeout' => 15 * 60 // 超时时间(单位:s)   

                      )

 

       );

       $context = stream_context_create($opts);

       $result = file_get_contents($url, false, $context);

 

       return $result;

 

 

    }

方法二:

         /**   

 * 发送post请求   

 *@param string $url 请求地址   

 *@param array $data_string post键值对数据   

 *@return string 

 */   

     publicfunction curl($url,$post_string)

         {

                   //echo$post_string;

                   $ch= curl_init();

       curl_setopt($ch, CURLOPT_URL, $url);

                   curl_setopt($ch,CURLOPT_POSTFIELDS, $post_string);

                   curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);

                   curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 10);

                   curl_setopt($ch,CURLOPT_TIMEOUT, 60);

                   $result= curl_exec($ch); 

                   curl_close($ch);

                   return$result;

                  

                  

 

                  

         }

public function curl ($url,$data_string)

         {

                   $ch= curl_init($url);

             curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");

                   curl_setopt($ch,CURLOPT_POSTFIELDS,$data_string);

                   curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

                   curl_setopt($ch,CURLOPT_HTTPHEADER, array(

                            'Content-Type:application/json',

                            'Content-Length:' . strlen($data_string))

                   );

                   //curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);

       //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

                   $result= curl_exec($ch);

                   $rtn=curl_getinfo($ch,CURLINFO_HTTP_CODE); 

             curl_close($ch);

             return $result;

         }


0 0
原创粉丝点击