我正在网站上构建字典。我将原始站点 (www.rae.es) 的术语定义称为术语定义,并将其 css 更改为我自己的一个。这是代码:
<?php
$word = $_REQUEST['word'];
$url = "http://lema.rae.es/drae/srv/search?val={$word}";
$css = <<<EOT
<style type="text/css">
@font-face {
font-family: 'brannboll_fetregular';
src: url('brannbollfet-webfont.eot');
src: url('brannbollfet-webfont.eot?#iefix') format('embedded-opentype'),
url('brannbollfet-webfont.woff') format('woff'),
url('brannbollfet-webfont.ttf') format('truetype'),
url('brannbollfet-webfont.svg#brannboll_fetregular') format('svg');
}
.a
{
color: #ffe200;
margin-top:20px !important;
}
.f
{
margin-left:20px !important;
margin-top:10px;
font-size: 200%;
color: #ffe200;
font-family: 'brannboll_fetregular';
}
.o
{
font-size: 80%;
color: #ffe200;
margin-left:10px;
font-family: 'Belgrano', serif;
}
.q {
margin-left:10px;
font-family: 'Belgrano', serif;
}
.k {
margin-left:10px;
font-family: 'Belgrano', serif;
}
.p {
font-family: 'Belgrano', serif;
}
img
{
visibility:hidden;
}
</style>
EOT;
$data = file_get_contents($url);
$data = str_replace('<head>', $css.'</head>', $data);
$data = str_replace('<span class="f"><b>.</b></span>', '', $data);
echo '<div id="result1"
style="
top: 77%;
left: 55%;
overflow:scroll;
width:400px;
height:300px;
border: 1px solid #000000;
border-radius: 15px;
background-opacity: 0.5;
background: #047C8F;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
box-shadow: inset 0px 3px 13px #000000;
-moz-box-shadow:
0px 3px 13px rgba(000,000,000,0.5),
inset 0px 0px 13px rgba(0,0,0,1);
-webkit-box-shadow:
0px 3px 13px rgba(000,000,000,0.5),
inset 0px 0px 13px rgba(0,0,0,1);
">
'.$data.'
</div>';
?>
现在,有时,对于某些词,会出现建议。例如:http://lema.rae.es/drae/?val=casa .如果你搜索“casa”,你会得到 3 个建议。现在,如果在我的站点中,我单击那些超链接,我将被重定向到一个错误页面。所以,我现在解决方案掌握在 php 手中,我只是不知道从哪里开始。 谢谢!
最佳答案
如果您查看从 http://lema.rae.es/drae/?val=casa 获得的 HTML ,你会看到你的建议链接是相对链接:
<a href="search?id=wHzIYMmRVDXX2tX1bpuy">
在将它们发送给用户之前,您应该将它们更改为绝对链接。你可以这样做:
<?php $data = preg_replace('/(search?[\d\w]+)/','http://lema.rae.es/drae/srv/\1', $data)
更好的解决方案是使用 DOMParser 来解析来自服务器的响应,并使用 CSS 选择器或 xpath 来查找链接并重写它们的 href 属性。看看 DOMCrawler Symfony component
关于php - 重定向两次到一个网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14098593/