【发布时间】:2018-12-04 09:44:28
【问题描述】:
我正在开发一个简单的 php 页面,它可以做到这一点:
- 从 url 查询字符串中获取搜索字符串(例如警察)
- 将搜索字符串附加到维基百科搜索 url (`https://en.wikipedia.org/w/index.php?search=police+officer')
- 使用 curl 获取该搜索字符串的最终重定向 URL
- 检查重定向的 URL 是否包含
index.php?search- 如果包含,则不执行任何操作 - 否则,爆炸重定向的url,从url中获取最后一个值(
Police_officer) - 将该值附加到返回该 wiki 记录的 JSON 数据的 Wikipedia URL (
https://en.wikipedia.org/api/rest_v1/page/summary/Police_officer) - 使用
file_get_contents()读取 JSON 数据并取回数据 - 例如title
由于某种原因,在这行代码中:
$json = file_get_contents($url_json);
在哪里 $url_json
https://en.wikipedia.org/api/rest_v1/page/summary/Santa_claus
我收到此错误:
Warning: file_get_contents(https://en.wikipedia.org/api/rest_v1/page/summary/Santa_claus): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in C:\xampp\public_html\test.php on line 49
但我可以在浏览器中访问该 URL,并查看与该 URL 相同类型的数据:
https://en.wikipedia.org/api/rest_v1/page/summary/Police_officer
对于那个,file_get_contents 返回数据就好了。
我使用了这个代码:
function get_http_response_code($url) {
$headers = get_headers($url);
return substr($headers[0], 9, 3);
}
确认两个页面的响应代码 = 200。
这是我的基本测试代码:
$var = $_GET['var'];
$var = str_replace(" ", "+", $var);
$url1 = "https://en.wikipedia.org/w/index.php?search=$var";
echo "<hr /> url1: $url1 <hr />";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$a = curl_exec($ch);
$redirected_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
echo "<hr /> url2: $redirected_url <hr />";
$url_search = strpos($redirected_url, "index.php?search");
echo "<hr /> url_search: $url_search <hr />";
function get_http_response_code($url) {
$headers = get_headers($url);
return substr($headers[0], 9, 3);
}
$url_response = get_http_response_code($redirected_url);
echo "<hr /> url_response: $url_response <hr />";
if ($url_search > 0) {
// do nothing
} else {
$tmp = explode('/', $redirected_url);
$end = end($tmp);
$url_json = "https://en.wikipedia.org/api/rest_v1/page/summary/$end";
echo "<hr /> url_json: $url_json <hr />";
$json = file_get_contents($url_json);
if ($json) {
$data = json_decode($json, TRUE);
if ($data) {
$wiki_page = $data['content_urls']['desktop']['page'];
echo "<hr /> wiki_page: $wiki_page <hr />";
}
}
}
我错过了什么?
【问题讨论】:
-
谢谢。最后我使用了
curl而不是file_get_contents,效果很好。为我的问题添加了解决方案。 -
应该添加答案还是要添加答案?
标签: php