使用curl访问接口
当我们需要给一些接口发送请求,请求数据的时候,我们可以使用curl来实现。
$url = "http://www.aaa.com/Api/?Uname=%s&Upass=%s&Mobile=%s&Content=%s";
$Uname = $Uname;
$Upass = $Upass;
$mobile = urlencode($mob);
$content = iconv("UTF-8", "GB2312", $content);
$url_content = urlencode($content);
$rurl = sprintf($url, $Uname, $Upass, $mobile, $url_content);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $rurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
上面代码中的$result为接口返回的结果,可以根据返回数据的格式进行相应的处理.