我在我的webapp中使用codeigniter 3.0。我想点击一个URL然后回到视图。让我们说点击一个按钮,我想点击一个API然后返回到相同的视图与成功消息。有人可以帮忙吗?
我的API为http://wmlab.in/api/getData.php?username=web
我尝试了文件('http://wmlab.in/api/getData.php?username=web'),但它无效。
答案 0 :(得分:1)
您可以使用file_get_contents('http://wmlab.in/api/getData.php?username=web')
或使用curl
;
对于Curl,请使用以下代码: -
$channel = curl_init();
curl_setopt( $channel, CURLOPT_URL, "http://wmlab.in/api/getData.php?username=web" );
curl_setopt( $channel, CURLOPT_RETURNTRANSFER, 1 );
$response= curl_exec ( $channel );
curl_close ( $channel );
// after curl response redirect to othe page
redirect('anypage');
对于文件获取内容,请使用以下代码: -
$response=file_get_contents('http://wmlab.in/api/getData.php?username=web')
// after response redirect to othe page
redirect('anypage');
希望它会对你有所帮助。
答案 1 :(得分:0)
使用curl执行此操作,如果您的服务器没有卷曲,也可以安装curl。
url="http://wmlab.in/api/getData.php?username=web";
function getUrlContent($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$data = curl_exec($ch);
echo $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return ($httpcode>=200 && $httpcode<300) ? $data : false;
}
$content=getUrlContent($url);
echo $content;
答案 2 :(得分:0)
它应该是工作
$result=file_get_contents('http://wmlab.in/api/getData.php?username=web');