我正在尝试搜索Wikipedia,并在php中获得结果。当我在php文件中以硬编码字符串形式输入URL时,它可以工作,但是如果我尝试将URL作为参数,它将返回以下内容:
["",[],[],[]]
这是带有硬编码字符串的代码:
<?php
$opts = array('http' =>
array(
'user_agent' => 'User1 (http://www.exampe.com/)'
)
);
$context = stream_context_create($opts);
$url = 'http://en.wikipedia.org/w/api.php?action=opensearch&search=yolo';
echo file_get_contents($url, FALSE, $context);
?>
以下是从其网址获取参数的版本:
<?php
$opts = array('http' =>
array(
'user_agent' => 'User1 (http://www.exampe.com/)'
)
);
$context = stream_context_create($opts);
$url = $_GET['url'];
echo file_get_contents($url, FALSE, $context);
?>
这就是我输入参数的方式:
http://example.com/test.php?url=http://en.wikipedia.org/w/api.php?action=opensearch&search=yolo
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句