• PHP发起POST DELETE GET POST 请求


    原文链接:http://blog.csdn.net/lengxue789/article/details/8254667

    关于POST,DELETE,GET,POST请求

    get:是用来取得数据。其要传递过的信息是拼在url后面,因为其功能使然,有长度的限制

    post:是用来上传数据。要上传的数据放在request的head里。没有长度限制。主要是用于增加操作

    put:也是用来上传数据。但是一般是用在具体的资源上。主要用于修改操作

    delete:用来删除某一具体的资源上。

    发起POST DELETE GET POST 请求通用类

     1 <?php
     2 class commonFunction{
     3     function callInterfaceCommon($URL,$type,$params,$headers){
     4         $ch = curl_init();
     5         $timeout = 5;
     6         curl_setopt ($ch, CURLOPT_URL, $URL);//目标地址
     7         //请求头
     8         if($headers!=""){
     9             curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
    10         }else {
    11             curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
    12         }
    13         curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);//返回结果,不输出
    14         curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);//超时时间
    15         //请求类型
    16         switch ($type){
    17             case "GET" :
    18             curl_setopt($ch, CURLOPT_HTTPGET, true);
    19             break;
    20             case "POST":
    21             curl_setopt($ch, CURLOPT_POST,true); 
    22             curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
    23             break;
    24             case "PUT" :
    25             curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 
    26             curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
    27             break;
    28             case "DELETE":
    29             curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 
    30             curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
    31             break;
    32         }
    33         $file_contents = curl_exec($ch);//获得返回值
    34         curl_close($ch);
    35         return $file_contents;
    36     }
    37 }
    38 ?>

    调用

    1 <?php
    2 $params="{user:"admin",pwd:"admin"}";
    3 $headers=array('Content-type: text/json',"id: $ID","key:$Key");
    4 $url=$GLOBALS["serviceUrl"]."/user";
    5 $strResult= spClass("commonFunction")->callInterfaceCommon($url,"PUT",$params,$headers);
    6 ?>

    $headers:如果参数值需要header传,可以以数组格式传递

  • 相关阅读:
    android openGl视频
    《jQuery权威指南》学习笔记之第2章 jQuery选择器
    RobHess的SIFT源码分析:综述
    building android/ubuntu-touch on 32bit machine
    Android开发(24)---安卓中实现多线程下载(带进度条和百分比)
    创建Sdcard
    POJ 1033 Defragment
    [C++STDlib基础]关于C标准输入输出的操作——C++标准库头文件<cstdio>
    机器学习实战决策树之眼镜男买眼镜
    protubuffer for windows配置指南!
  • 原文地址:https://www.cnblogs.com/hubery/p/4818707.html
Copyright © 2020-2023  润新知