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);
相关文章
相关标签/搜索