php-curl小记


用jQuery:

 

 1 $.ajax({
 2   url:url,
 3   type:"POST",
 4   data:data,
 5   contentType:"application/json; charset=utf-8",
 6   dataType:"json",
 7   success: function(){
 8     ...
 9   }
10 })

php curl (自定义post请求  设置header头信息)

 1 $data = array("name" => "Hagrid", "age"=> "36");                                                                    
 2 $data_string = json_encode($data);     
 3 $ch = curl_init('http://api.local/rest/users');      
 4 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                          
 5 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
 6 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 7 curl_setopt($ch, CURLOPT_HTTPHEADER, array(                 
 8     'Content-Type: application/json',
 9     'Content-Length: ' . strlen($data_string))         
10 );                                                                                                                   
11 $result = curl_exec($ch);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM