对 post 提交的数据进行 http_build_query处理,然后再send出去,能实现更好的兼容性,更小的请求数据包。
<?php /** * PHP发送Post数据 * @param string $url 请求url * @param array/string $params 发送的参数 * @return array */ function http_post_data($url, $params = array()) { if (is_array($params)) { $params = http_build_query($params, null, ‘&‘); } $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return array($httpCode, $response); } $url = "http://blog.snsgou.com"; $data = array(‘a‘ => 1, ‘b‘ => 2, ‘c‘ => 2); list($returnCode, $returnContent) = http_post_data($url, $data);
原文:http://www.cnblogs.com/yudis/p/5608910.html