修正了我用卷发代替文件获取内容
$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);
}
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$html = curl_exec($ch);
curl_close($ch);
return $html;
}
$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);
$json = file_get_contents_curl($url_json);
echo "<hr /> json: $json <hr />";
if ($json) {
$data = json_decode($json, TRUE);
echo "<hr /> data: $data <hr />";
if ($data) {
$wiki_page = $data['content_urls']['desktop']['page'];
echo "<hr /> wiki_page: $wiki_page <hr />";
}
}
}