GET 数据请求
<span style="font-size:18px;">$url = "http://www.xxxx.com?user=111"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); $out = curl_exec($ch); curl_close($ch); $obj = json_decode($out); print $obj;</span>
POST 数据发送
<span style="font-size:18px;">$url = "http://www.xxxx.com?user=111"; $post_data = "somedata"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch , CURLOPT_POST , 1 ) ; curl_setopt($ch , CURLOPT_POSTFIELDS , $post_data ) ; $out = curl_exec($ch); curl_close($ch); $obj = json_decode($out); print $obj; </span>
http://php.net/manual/zh/function.curl-setopt.php 可以去文档中去 找寻更多的选项。
有一次还用到 这两个 选项
<span style="font-size:18px;"> curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);</span>反正在 POST不到数据的时候,要多看看选项。
POST 数据接收
<span style="font-size:18px;">$post_data = file_get_contents("php://input"); $post_data = trim($post_data); print $post_data;</span>