如何解决如何使用curl下载IP2Location数据库
我正在尝试使用他们提供的curl命令从IP2Location下载数据库。我已经注册,所以我有一个有效的令牌。他们给出的命令是
curl -o {LOCAL_FILE_NAME} "https://www.ip2location.com/download?token={DOWNLOAD_TOKEN}&file={DATABASE_CODE}"
这是我使用的代码,除了令牌:
$curl = curl_init();
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_URL,"https://www.ip2location.com/download?token={mytoken}&file={DB1LITEBIN}");
$dbfile = curl_exec($curl);
if (curl_errno($curl)) {
echo 'err '.curl_errno($curl);
}
curl_close($curl);
$file = 'db_download.bin';
$mode = 'w';
if (($fp = fopen($file,$mode))) {
$fout = fwrite($fp,$dbfile);
fclose($fp);
}
脚本运行无错误,但下载的文件只是其站点的未找到页面。如果在浏览器中使用该网址,则会得到找不到相同页面的信息。有人可以指出我的错误吗?
解决方法
请尝试以下方法。请注意,您将下载一个包含bin文件的zip文件。
<?php
//The resource that we want to download.
$fileUrl = 'https://www.ip2location.com/download?token=XXXXXXXXXX&file=DB1LITEBIN';
//The path & filename to save to.
$saveTo = 'db_download.zip';
//Open file handler.
$fp = fopen($saveTo,'w+');
//If $fp is FALSE,something went wrong.
if($fp === false){
throw new Exception('Could not open: ' . $saveTo);
}
//Create a cURL handle.
$ch = curl_init($fileUrl);
//Pass our file handle to cURL.
curl_setopt($ch,CURLOPT_FILE,$fp);
//Timeout if the file doesn't download after 20 seconds.
curl_setopt($ch,CURLOPT_TIMEOUT,20);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
//Execute the request.
curl_exec($ch);
//If there was an error,throw an Exception
if(curl_errno($ch)){
throw new Exception(curl_error($ch));
}
//Get the HTTP status code.
$statusCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
//Close the cURL handler.
curl_close($ch);
//Close the file handler.
fclose($fp);
if($statusCode == 200){
echo 'Downloaded!';
} else{
echo "Status Code: " . $statusCode;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。