【发布时间】:2015-05-26 06:07:11
【问题描述】:
我正在开发我尝试使用 curl 的应用程序。在此之前我尝试使用 file_get_contents() 但由于 allow_url 问题而无法在我的服务器上运行(我尝试联系托管但尚未解决所以我尝试替代)。所以我使用 curl 从远程站点获取数据。我使用此代码获取数据:
$url="http://api.hostip.info/get_html.php?ip=182.188.193.238";
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
print_r($result);
当我打印时,我得到Country: PAKISTAN (PK) City: Lahore IP: 182.188.193.238。我想从这个字符串中得到国家。我试过这样但得到像$data = json_decode($result,true); 这样的空结果。 Json 解码返回空结果。我认为唯一的方法是破坏该字符串?感谢您提前提供任何提示。我想从结果中获取国家/地区名称。
【问题讨论】:
-
那不是 JSON。因此,您必须使用带有正则表达式的
preg_match()才能从响应中获取它。