使用PHP解析网页内容
我认为这是一个简单的问题,但我已经做了我所知道的,但仍然没有工作。我希望从此链接获取输出: 您可以粘贴到浏览器上并查看它。有一些文本输出。我尝试过PHP中的一些函数,比如file_get_contents和curl。我没有使用ajax或JavaScript,因为我不是这方面的专家。最后,我正在与XAMPP合作。使用PHP解析网页内容,php,file,curl,file-get-contents,php-parser,Php,File,Curl,File Get Contents,Php Parser,我认为这是一个简单的问题,但我已经做了我所知道的,但仍然没有工作。我希望从此链接获取输出: 您可以粘贴到浏览器上并查看它。有一些文本输出。我尝试过PHP中的一些函数,比如file_get_contents和curl。我没有使用ajax或JavaScript,因为我不是这方面的专家。最后,我正在与XAMPP合作。 $op=file_get_contents('http://api.microsofttranslator.com/V2/Ajax.svc/Translate?text=siapa+r
$op=file_get_contents('http://api.microsofttranslator.com/V2/Ajax.svc/Translate?text=siapa+rektor+ipb&appId=58C40548A812ED699C35664525D8A8104D3006D2&from=id&to=en');
echo $op;
有时特殊字符可能会影响您的实际输出,以下是使用干净文本的解决示例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<?php
$url = 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate?text=siapa+rektor+ipb&appId=58C40548A812ED699C35664525D8A8104D3006D2&from=id&to=en';
$content = file_get_contents($url);
echo $content;
?>
</html>
无标题文件
如果我能为您提供更多帮助,请告诉我。
。。。我试过一些函数…
你到底试过什么?你能提供一些代码吗?对我来说,file\u get\u contents($url)
显示了“谁是大学校长”
。它按提供的方式工作。yap I get无法打开流:无法建立连接,因为目标计算机在使用文件\u get\u内容时主动拒绝。总的来说,我尝试了下面的所有答案。可能是网络问题。是的,可能是因为我办公室的代理$content
将包含页面的全部内容。你能告诉我如何将文本内容转换成$content吗?
$url = 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate?text=siapa+rektor+ipb&appId=58C40548A812ED699C35664525D8A8104D3006D2&from=id&to=en';
// using file_get_contents function
$content = file_get_contents($url);
echo $content;
#output# "who is the Rector of the University"
// using file function // read line by line in array
$content = file($url);
print_r($content);
#output# Array (0] => "who is the Rector of the University")
// using cURL
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
echo $content;
#output# "who is the Rector of the University"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<?php
$url = 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate?text=siapa+rektor+ipb&appId=58C40548A812ED699C35664525D8A8104D3006D2&from=id&to=en';
$content = file_get_contents($url);
echo $content;
?>
</html>