PHP-网络请求 GETPOSTPUTDELETE

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP-网络请求 GETPOSTPUTDELETE相关的知识,希望对你有一定的参考价值。

<?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);   } } ?>


调用:

 

  $params="{user:\"admin\",pwd:\"admin\"}";
  $headers=array(‘Content-type: text/json‘,"id: $ID","key:$Key");
  $url=$GLOBALS["serviceUrl"]."/user";
  $strResult= spClass("commonFunction")->callInterfaceCommon($url,"PUT",$params,$headers); 

 

 

  

以上是关于PHP-网络请求 GETPOSTPUTDELETE的主要内容,如果未能解决你的问题,请参考以下文章

c# 后台GETPOSTPUTDELETE传输发送json数据

Python接口测试自动化说明及代码实例:含getpostputdelete等方法

iOS项目相关@AFN&SDWeb的二次封装

C 语言在Linux下发送HTTP 请求

.net web api 一

PHP-02.文件上传php保存/转移上传的文件常见的网络传输协议请求报文及属性响应报文及属性