POST XML to URL with PHP and Handle Response

后端 未结 5 966
天命终不由人
天命终不由人 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:01

    There isn't really a standard way. In code meant for distribution, I generally check cURL, file_get_contents and sockets, using the first one found. Each of those supports GET and POST, and each of those may or may not be available (or work) depending on the PHP version and configuration.

    Basically something like:

    function do_post($url, $data) {
      if (function_exists('curl_init') && ($curl = curl_init($url))) {
        return do_curl_post($curl, $data);
      } else if (function_exists('file_get_contents') && ini_get('allow_url_fopen') == "1") {
        return do_file_get_contents_post($url, $data);
      } else {
        return do_socket_post($url, $data);
      }
    }
    

提交回复
热议问题