php获取其他接口的返回值

这个果然很好用:
[url]http://zhidao.baidu.com/question/444120411.html[/url]

function make_request($url, $params , $timeout =30){
set_time_limit(0);
$str="";
if($params!="")
{
foreach ($params as $k=>$v) {
if (is_array($v)) {
foreach ($v as $kv => $vv) {
$str .= '&' . $k . '[' . $kv . ']=' . urlencode($vv);
}
} else {
$str .= '&' . $k . '=' . urlencode($v);
}
}
}
if (function_exists('curl_init')) {
// Use CURL if installed...
$ch = curl_init();
$header=array(
'Accept-Language: zh-cn',
'Connection: Keep-Alive',
'Cache-Control: no-cache'
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
if($timeout > 0)curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$result = curl_exec($ch);
$errno = curl_errno($ch);
curl_close($ch);
return $result;
} else {
$context = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded'."\r\n".
'Content-length: ' . strlen($str),
'content' => $str));
if($timeout > 0)$context['http']['timeout'] = $timeout;
$contextid = stream_context_create($context);
$sock = @fopen($url, 'r', false, $contextid);
if ($sock) {
$result = '';
while (!feof($sock)) {
$result .= fgets($sock, 8192);
}
fclose($sock);
}
else{
return 'TimeOut';
}
}
return $result;
}

3个参数:
1 你要访问的页面的url地址。
2 你的请求参数:array(id=>"1",name=>'root'); 按照这样的类型
3 超时时间 默认30秒 很好用的
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值