【发布时间】:2021-06-30 22:32:18
【问题描述】:
我正在使用 api 来使用 ip 地址检测用户的位置。为此,我想使用我在下面编写的 cURL 函数,而不是使用 file_get_contents。但是我得到的字符串非常复杂。如何将此字符串转换为清晰的数组?
我的代码
function curl_get_contents($url) {
// Initiate the curl session
$ch = curl_init();
// Set the URL
curl_setopt($ch, CURLOPT_URL, $url);
// Removes the headers from the output
curl_setopt($ch, CURLOPT_HEADER, 0);
// Return the output instead of displaying it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute the curl session
$output = curl_exec($ch);
// Close the curl session
curl_close($ch);
// Return the output as a variable
return $output;
}
$output = curl_get_contents("http://ip-api.com/php/".$ip);
echo $output;
结果
a:14:{s:6:"status";s:7:"success";s:7:"country";s:6:"Turkey";s:11:"countryCode";s:2:"TR";s:6:"region";s:2:"35";s:10:"regionName";s:5:"Izmir";s:4:"city";s:5:"Izmir";s:3:"zip";s:5:"35600";s:3:"lat";d:38.4667;s:3:"lon";d:27.1333;s:8:"timezone";s:15:"Europe/Istanbul";s:3:"isp";s:11:"TurkTelecom";s:3:"org";s:0:"";s:2:"as";s:18:"AS47331 TTNet A.S.";s:5:"query";s:13:"85.107.65.120";}
【问题讨论】:
标签: php arrays curl file-get-contents