【问题标题】:cURL using info from mySQL, then storing the cURL'ed infocURL 使用来自 mySQL 的信息,然后存储 cURL 的信息
【发布时间】:2015-10-28 04:27:25
【问题描述】:

我正在用 PHP 编程。

到目前为止,我发现一篇很有用的文章主要是关于如何通过一个包含大量信息的站点进行 CURL,但我真正需要的是如何在没有太多信息的多个站点上进行 cURL - 几行,如事实上!

另一部分是,文章的重点主要是存储在FTP服务器的txt文件中,但是我已经将大约900个地址加载到mysql中,并希望从那里加载它们,并用存储的信息丰富表格在链接中 - 我将在下面提供!

我们有一些开放的公共图书馆,其中包含地址和相关信息以及 API。

Link到主站:

我要使用的函数:http://dawa.aws.dk/adresser/autocomplete?q=

SQL 结构: 数据示例:http://i.imgur.com/jP1J26U.jpg

fx 此地址: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 结构所示?

【问题讨论】:

  • 所以显示你已经尝试更新数据库的代码。
  • 返回的格式称为 JSON,php 完全可以理解,那么为什么要将它转储到 BLOB 中呢?抱歉,您在这里并没有真正的意义......
  • 这可以在大约 10 行代码中完成,但我需要更多信息来帮助。您目前在您的数据库中有哪些数据(例如存储在 AdrName 和 AdrId 中的数据)?生成搜索网址是否足够,或者搜索字段(?q= 之后的位)是否来自其他地方?您目前如何访问您的数据库?
  • @Steve: AdrID 是空的,这是我要插入输出的地方。 AdrName 是地址 fx "Dornen 2 6715 Esbjerg N" 是的,这就足够了。我在 AdrName 中有我的地址列表,应该在 ?q= 我通过 phpmyadmin 访问我的数据库 - 我不太确定这是否是您问题的答案?
  • @fvu:如果它更容易做的话——就像我说的,我是 cURL 的新手。对不起,如果没有意义:(

标签: php mysql api curl


【解决方案1】:

如果你想在 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));

【讨论】:

  • 如何循环访问我的数据库并保存地址?
【解决方案2】:

您下载的数据是 json,因此您可以将其存储在 varchar 列而不是博客中。 此外,带有 api 的站点似乎并不在意 http 引荐来源网址、用户代理等,因此您可以使用 file_get_contents 代替 curl。

因此,只需从您的数据库中获取所有结果,对其进行迭代,调用 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 
}

【讨论】:

  • 我试过做你的代码,但有些失败了!代码:mesmerize.dk/curl/index2.phps 测试站点:mesmerize.dk/curl/index2.php 第 20 行失败是这样的: $sql = ('UPDATE AdrCheck SET status="" WHERE id=""', [$apidata, $searchTerm]); 我不知道是不是因为我又笨又累了! :(
  • @PKPX10 pastebin.com/z4NLajfj 请注意 mysql_* 函数已弃用。你会更好地使用 PDO 和准备好的语句
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-24
  • 1970-01-01
相关资源
最近更新 更多