如何使用php解决whit@file_get_contents的敏感事例url问题


How to solve sensitive case url issue whit @file_get_contents using php?

如何使用php解决@file_get_contents的敏感案例url问题?

使用小写url example.com 时效果良好

像这个代码

$json = @file_get_contents("https://www.example.com/test.php?word=example.biz", true);

但在第一个url使用大写Example.com 时不起作用

像这个代码

$json = @file_get_contents("https://www.Example.com/test.php?word=example.biz", true);

如何解决这个问题,以便更好地处理小写和大写?

所有域都是小写的,但querystring及其路径可能是大写的。所以我们不能将整个url转换为小写,而只能转换为domain。

所以下面的代码只会将域转换为小写,并保持路径和查询字符串组件的其余部分不变

<?php
$url = "https://www.Example.com/test.php?word=example.biz";

$parse_url = parse_url($url);
//print_r($parse_url);die;
$new_url = $parse_url['scheme'].'://'.strtolower($parse_url['host']).$parse_url['path'].'?'.$parse_url['query'];


$json = @file_get_contents($new_url, true);
?>