码迷,mamicode.com
首页 > Web开发 > 详细

PHP发起get post put delete请求

时间:2015-11-03 10:23:49      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

<?php   
class commonFunction{  
    function callInterfaceCommon($URL,$type,$params,$headers){  
        $ch = curl_init();  
        $timeout = 5;  
        curl_setopt ($ch, CURLOPT_URL, $URL); //发贴地址  
        if($headers!=""){  
            curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);  
        }else {  
            curl_setopt ($ch, CURLOPT_HTTPHEADER, array(‘Content-type: text/json‘));  
        }  
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
        switch ($type){  
            case "GET" : curl_setopt($ch, CURLOPT_HTTPGET, true);break;  
            case "POST": curl_setopt($ch, CURLOPT_POST,true);   
                         curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break;  
            case "PUT" : curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT");   
                         curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break;  
            case "DELETE":curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE");   
                          curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break;  
        }  
        $file_contents = curl_exec($ch);//获得返回值  
        return $file_contents;  
        curl_close($ch);  
    }  
}  
  

  

PHP发起get post put delete请求

标签:

原文地址:http://www.cnblogs.com/adtuu/p/4932244.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!