友善交流技术...

PHP CURL API 接口测试

上一篇 / 下一篇  2011-07-28 18:11:50 / 个人分类:单元测试

class cmsApi {

//初始化url参数
 function  urlinit($app_url,$url){
  $ch = curl_init();
  curl_setopt ($ch, CURLOPT_URL, $app_url.$url); //REST地址 GET  
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 15); 
  return $ch ;
 }
 
//JSON 数据格式 
 function xml_to_json($source) { 
 if(is_file($source)){             //传的是文件,还是xml的string的判断 
  $xml_array=simplexml_load_file($source); 
 }else{ 
  $xml_array=simplexml_load_string($source); 
 } 
 $json = json_encode($xml_array);  //php5,以及以上,如果是更早版本,请下载JSON.php 
 return $json; 
   }
 
//【CMS-HLD_0.011】获取billingcode
 function getBillingcode_011(){
     // Get method
    //$app_url = "http://IP:8081/";
    $app_url = "http://IP:9081/";
    $url ='conferenceReservManager/getBillingcode/' ;
  //print_r($post_data);
     $ch = $this->urlinit($app_url,$url);
  curl_setopt($ch, CURLOPT_GET, 1);   
  $file_contents = curl_exec($ch);//获得返回值
  curl_close($ch);  
  return $file_contents; 
 }
 

// getPasscode_012
 function  getPasscode_012($num){
  // Get method
    $app_url = "http://ip:9081/";
    $url = 'conferenceReservManager/getPasscode/'.$num;
    //echo $url ;   
  //初始化URL
     $ch = $this->urlinit($app_url,$url);
    
  curl_setopt($ch, CURLOPT_GET, 1);    
  $file_contents = curl_exec($ch);//获得返回值
  curl_close($ch);  
  return $file_contents; 
 }
//获取acquirePin
 function  acquirePin_014($billingcode){
       // Get method
    $app_url = "http://ip:9081/";
    $url = 'conferenceReservManager/acquirePin/'.$billingcode;  

  //初始化URL
     $ch = $this->urlinit($app_url,$url);  
  curl_setopt($ch, CURLOPT_GET, 1);   
  $file_contents = curl_exec($ch);//获得返回值
  curl_close($ch);  
  return $file_contents; 
 }
 
//reservConference
 function reservConference_007($billingcode,$pcode1,$pcode2){
  
   // Get method
    $app_url = 'http://ip:9081/cmsrest/rs/';
    $url = 'conferenceReservManager/reservConference/'; 
    
    $post_data ='{"xmlTemplateName":"/root/sme.xml","userId":"","applicationId":"SME-MEETING","value":{"2":"3,4","7":"23"},
    "conference":{"valid":0,"noticetime":0,"conferenceuservalues":{},"callout":false,
    "starttime":"2011-07-29T18:57:22Z","reservtime":"2011-07-26T18:57:22Z","conferencedesc":"","confscale":4,
    "billingcode":"'.$billingcode.'","password":"","product":{"productroles":{},"productid":3,"conferences":{},
    "conferenceservices":{}},"conferenceminutes":120,"pin":false,"roles":"4,5","conferencename":" tester 2011/7/30 01",
    "language":2,"realreserve":true},"pcode1":"'.$pcode1.'","pcode2":"'.$pcode2.'"}';
    
  //初始化URL
     $ch = $this->urlinit($app_url,$url);      
     $datatype ='application/json';
    $accepttype='text/plain';
     curl_setopt($ch, CURLOPT_POST,1); //设置POST方式
     curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); //发送的post值,目前已经放入url
     curl_setopt($ch, CURLOPT_HTTPHEADER, array (
          "Content-Type: ".$datatype."; charset=utf-8",
          "accept: ".$accepttype."; charset=utf-8"));  
  $file_contents = curl_exec($ch);//获得返回值
  curl_close($ch);  
  return $file_contents; 
 }
 
 
//【CMS-HLD_0.001】userJoin
 function userJoin_001($LshyID,$pinCode){
       // Get method
     $app_url = "http://IP:9081/";
    $url = 'conferenceBusiness/userJoin/SME-MEETING/'.$LshyID;
    
     $post_data=$this->xml_to_json('<userDTO><clientType>2</clientType><domain>1</domain><ipaddr>192.168.12.155</ipaddr>
    <isowner>true</isowner><pinCode>'.$pinCode.'</pinCode><rolemap><entry><key>4</key><value/></entry><entry><key>7</key><value/>
    </entry><entry><key>8</key><value/></entry><entry><key>9</key><value/></entry><entry><key>10</key><value/></entry><entry>
    <key>11</key><value/></entry><entry><key>12</key><value/></entry><entry><key>13</key><value/></entry><entry><key>14</key><value/>
    </entry></rolemap><tempuserid>4</tempuserid><userName>liming</userName><userStatus>0</userStatus></userDTO>');
      
     //初始化URL
     $ch = $this->urlinit($app_url,$url);
  $datatype='application/json';
  $accepttype='application/json';
  //设置POST方式
   curl_setopt($ch, CURLOPT_POST, true);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); //发送的post值,目前已经放入url
   curl_setopt($ch, CURLOPT_HTTPHEADER, array (
          "Content-Type: ".$datatype."; charset=utf-8",
          "accept: ".$accepttype."; charset=utf-8"));
       
  $file_contents = curl_exec($ch);//获得返回值
  curl_close($ch);  
  return $file_contents; 
 }
 }

 备注几点:

1. GET 和 POST 提交是有区别的:

   其中get 请求的数据一般比较小的.POST提交的方法,可以提交比较大的数据量

2. POST 方法提交

   注意数据格式和接受数据格式:如果错了,提交也会失败的.

 


TAG:

 

评分:0

我来说两句

Open Toolbar