我有一个简单的函数返回外部网页的标题。
public function actionGetUrl($url)
{
$str = file_get_contents($url);
// index of the opening-tag + the length of needle
$openTag = strpos($str,"<title>") +7;
// index of the ending tag
$endTag = strpos($str,"</title>");
// extract the bit between
$title = substr($str, $openTag, ($endTag - $openTag));
return $title;
}
在我的jquery函数中,我有以下内容,
$(document).ready(function(){
$(function(){
$("#grabUrl").click(function(e){
$.ajax({
url: "<?php echo Yii::app()->createUrl('post/geturl', array('url' => 'http://bbc.co.uk')); ?>",
success: function(data) {
strang= String(data);
alert(strang);
}
});
});
});
});
警报不显示任何内容。在这种情况下,$ title包含'BBC主页'。
有人看到我弄错了吗?
感谢您的帮助
答案 0 :(得分:0)
假设您在PHP函数中所做的事情是正确的。你不必在php中返回数据,只需回应响应。
public function actionGetUrl($url)
{
$str = file_get_contents($url);
// index of the opening-tag + the length of needle
$openTag = strpos($str,"<title>") +7;
// index of the ending tag
$endTag = strpos($str,"</title>");
// extract the bit between
$title = substr($str, $openTag, ($endTag - $openTag));
echo $title;
}
答案 1 :(得分:0)
你正在使用ajax,所以从php不会返回echo $title;
。
public function actionGetUrl($url)
{
$str = file_get_contents($url);
// index of the opening-tag + the length of needle
$openTag = strpos($str,"<title>") +7;
// index of the ending tag
$endTag = strpos($str,"</title>");
// extract the bit between
$title = substr($str, $openTag, ($endTag - $openTag));
echo $title;
}