php模擬post提交

由於要給android和ios端的寫接口,原本調試用的是寫入文件的方式,比較麻煩,就從網上拔了curl模擬post提交的方法,來進行客戶端模擬測試。貼上php


代碼以供往後使用。android

<?php
//要請求的內容

    
    $post_data = array(); 
    $key = 'baichi'; 

    $json_data['server_key'] =    strtoupper(md5($key));  
      $json_data['m']='depart';
      $json_data['a']='insert';
    $json_data['user_name'] ='439741';
    $json_data['user_pwd'] = 'zxc5739701';
  $json_data['email'] = '345579160@qq.com';
  $json_data['city_id'] = '1';
  $json_data['address'] = 'adas';
  $json_data['mobile_phone'] = '110110';
    $url='http://127.0.0.1/fanwe4.3/mobilecode.php'; 
    $data_string = json_encode($json_data);
//模擬post提交
    /*
   
     
    $o="";  
    foreach ($post_data as $k=>$v)  
    {  
        $o.= "$k=".urlencode($v)."&";  
    }  
    $post_data=substr($o,0,-1);  
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_POST, 1);  
    curl_setopt($ch, CURLOPT_HEADER, 0);  
    curl_setopt($ch, CURLOPT_URL,$url);  
    //爲了支持cookie  
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');  
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
*/
    //模擬客戶端json提交
    $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))
    );


    $result = curl_exec($ch);  
    print_r($result);
相關文章
相關標籤/搜索