POST XML to URL with PHP and Handle Response

后端 未结 5 959
天命终不由人
天命终不由人 2020-12-11 11:16

I\'ve seen numerous methods of POSTing data with PHP over the years, but I\'m curious what the suggested method is, assuming there is one. Or perhaps there is a somewhat uns

5条回答
  •  抹茶落季
    2020-12-11 12:09

    While the Snoopy script maybe cool, if you're looking to just post xml data with PHP, why not use cURL? It's easy, has error handling, and is a useful tool already in your bag. Below is an example of how to post XML to a URL with cURL in PHP.

    // url you're posting to        
    $url = "http://mycoolapi.com/service/";
    
    // your data (post string)
    $post_data = "first_var=1&second_var=2&third_var=3";
    
    // create your curl handler     
    $ch = curl_init($url);
    
    // set your options     
    curl_setopt($ch, CURLOPT_MUTE, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //ssl stuff
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:  application/x-www-form-urlencoded'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    // your return response
    $output = curl_exec($ch); 
    
    // close the curl handler
    curl_close($ch);
    

提交回复
热议问题