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