我正在用PHP编程。
我发现到目前为止有用的文章主要是关于如何通过一个包含大量信息的网站进行CURL,但我真正需要的是如何在没有太多信息的多个网站上进行cURL - 几行,如事实上!
另一部分是,文章重点主要是将它存储在FTP服务器的txt文件中,但我已经将大约900个地址加载到mysql中,并希望从那里加载它们,并用存储的信息丰富表格在链接中 - 我将在下面提供!
我们有一些开放的公共图书馆,其中包含有关这些的地址和信息以及API。
Link到主站点:
我想使用的功能:http://dawa.aws.dk/adresser/autocomplete?q=
SQL结构:
数据示例:http://i.imgur.com/jP1J26U.jpg
fx this addresse:Dornen 2 6715 Esbjerg N(在数据库中称为AdrName)。
http://dawa.aws.dk/adresser/autocomplete?q=Dornen%202%206715%20Esbjerg%20N
这将给我以下输出(我想存储在数据库中的AdrID中):
[
{
"tekst": "Dornen 2, Tarp, 6715 Esbjerg N",
"adresse": {
"id": "0a3f50b8-d085-32b8-e044-0003ba298018",
"href": "http://dawa.aws.dk/adresser/0a3f50b8-d085-32b8-e044-0003ba298018",
"vejnavn": "Dornen",
"husnr": "2",
"etage": null,
"dør": null,
"supplerendebynavn": "Tarp",
"postnr": "6715",
"postnrnavn": "Esbjerg N"
}
}
]
如何将所有内容存储在blob中,如SQL结构中所示?
答案 0 :(得分:0)
如果你想在php中发出cURL请求,请使用此方法
function curl_download($Url){
// is cURL installed yet?
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
// OK cool - then let's create a new cURL resource handle
$ch = curl_init();
// Now set some options (most are optional)
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);
// Set a referer
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
return $output;
}
然后你用
调用它print curl_download('http://dawa.aws.dk/adresser/autocomplete?q=Melvej');
或者你可以直接转换它jSON对象
$jsonString=curl_download('http://dawa.aws.dk/adresser/autocomplete?q=Melvej');
var_dump(json_decode($jsonString));
答案 1 :(得分:0)
您下载的数据是json,因此您可以将其存储在varchar列而不是博客中。
此外,带有api的网站似乎对http referrer,用户代理等不感兴趣,因此您可以使用file_get_contents
代替curl。
因此,只需从db中获取所有结果,迭代它们,调用api,并使用正确的数据更新相应的行:
//get all the rows from your database
$addresses = DB::exec('SELECT * FROM addresses'); //i dont know how you actually access your db, this is just an example
foreach($addresses as $address){
$searchTerm = $address['AdrName'];
$addressId = $address['Vid'];
//download the json
$apidata = file_get_contents('http://dawa.aws.dk/adresser/autocomplete?q=' . urlencode($searchTerm));
//save back to db
DB::exec('UPDATE addresses SET status=? WHERE id=?', [$apidata, $searchTerm]);
//if you want to access the data, you can use json_decode:
$data = json_decode($apidata);
echo $data[0]->tekst; //outputs Dornen 2, Tarp, 6715 Esbjerg N
}