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);

 

posted @ 2014-01-23 11:42  糖糖果  阅读(188)  评论(15编辑  收藏  举报