我正在尝试从API调用信息来检索$ baseURL中的项目数据。
$url1
用于获取可用性json数据。
$url2
用于获取单个项目的json数据。例如:内容,图像,多媒体等。
<?php
$APIKEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$baseURL = 'http://feeds.example.com/v1/';
$ext = '.json';
// calls latest updated items
$url1 = 'folder';
// calls specific ID of item with corresponding details
$url2 = 'folder/item{id}';
$ch1 = curl_init($baseURL . $url1 . $ext);
$ch2 = curl_init($baseURL . $url2 . $ext);
$headers = array();
$headers[] = 'x-auth-token:' . $APIKEY;
$headers[] = 'X-Affiliate-Authentication:'. $APIKEY;
$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=utf-8';
curl_setopt($ch1, CURLOPT_USERPWD, $APIKEY);
curl_setopt($ch1, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_USERPWD, $APIKEY);
curl_setopt($ch2, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
$mh = curl_multi_init();
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);
$active = null;
do {
curl_multi_exec($mh, $active);
} while ($active);
$response_1 = curl_multi_getcontent($ch1);
$response_2 = curl_multi_getcontent($ch2);
echo "$response_1 $response_2";
// Add function here
// Close function here
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
?>
'response_1'输出(符合预期):
{"data":[{"id":2,"last_update":"2016-08-26 15:43:39"},
{"id":6,"last_update":"2016-08-26 08:57:53"},
{"id":7,"last_update":"2016-08-23 16:49:15"},
{"id":8,"last_update":"2016-08-19 10:20:00"},
{"id":9,"last_update":"2016-08-28 23:44:25"},
{"id":28,"last_update":"2016-08-27 12:45:16"},
{"id":29,"last_update":"2016-08-12 07:55:32"},
{"id":30,"last_update":"2016-07-29 11:00:23"},
{"id":31,"last_update":"2016-07-05 09:48:50"}
.
..
...
'response_2'输出(预期):
{"data":[
{
"id":2,"last_update":"2016-08-26 15:43:39",
"address":
{
"address1":"Foo Bar",
"address2":"Foo","city":"Bar","state":"FooBar","zip_code":"F00 84R","country":"Foo Bar",
},
"details":
{
"dwelling_name":"FooBar Hotel","hotel_type":"Big",
"maximum_capacity":16,
"base_capacity":16,
"bedrooms":6,
"bathrooms":3,
"currency":"GBP"
},
.
..
...
我需要确定动态更新$ url []的最佳方法。以下是我的逻辑,如果你运行它将无法工作,因为它当然只是我的笔记......
// folder/item{id}
$url[];
// Retrieved from 3rd party API for comparison
$storedUpdate;
foreach {
item{id}
if $storedUpdate and item{id} matches item{id} from $url1 {
do nothing
else {
POST item{id} data to 3rd party API
POST $latestUpdate to 3rd party API
}
};
我之所以选择curl_multi_init是因为它能够(通过阅读它)以异步方式解析数据。由于项目{ids}容易超过3500,将这些手动编码到我的工作中似乎是不合逻辑的,这就是为什么我正在考虑动态地执行它的合理性。
我一直在搜索互联网,论坛和php curl手册,以找到动态更新$ url2的方法,以便它获取GET的所有数据。
我相信我可以使用$ url []输入多个网址,问题似乎仍然是因此我必须手动输入这些网址。
feeds.example.com/v1/folder/{id}.json
我不会用这个来绕过丛林,我已经在很短的时间内学会了这一点,并且在我的结果中试图确定实现目标的最佳实践。
非常感谢任何帮助。
答案 0 :(得分:1)
如果我理解正确,那么你的逻辑就会稍微偏离。您的第二个请求取决于第一个请求中返回的信息,因此您需要同步流而不是异步流。要异步执行此操作需要...时间旅行。
如果您仍希望获得curl_multi...
功能的好处,则必须将其限制为第二个请求。例如:
<?php
$APIKEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$baseURL = 'http://feeds.example.com/v1/';
$ext = '.json';
// calls latest updated items
$url = 'folder';
$ch1 = curl_init($baseURL . $url . $ext);
$headers = array();
$headers[] = 'x-auth-token:' . $APIKEY;
$headers[] = 'X-Affiliate-Authentication:'. $APIKEY;
$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=utf-8';
curl_setopt($ch1, CURLOPT_USERPWD, $APIKEY);
curl_setopt($ch1, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
$response_1 = curl_exec($ch1);
$response_1_json = json_decode($response_1, true);
$response_1_data = $response_1_json['data'];
$mh = curl_multi_init();
$multiHandles = [];
foreach ($response_1_data as $key => $item) {
$id = $item['id'];
$multiHandles[$key] = curl_init($baseURL . 'folder/item' . $id . $ext);
curl_setopt($multiHandles[$key], CURLOPT_USERPWD, $APIKEY);
curl_setopt($multiHandles[$key], CURLOPT_HTTPHEADER, $headers);
curl_setopt($multiHandles[$key], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh, $multiHandles[$key]);
}
$active = null;
do {
curl_multi_exec($mh, $active);
} while ($active);
$response_2 = "";
foreach ($response_1_data as $key => $item) {
$response_2 .= curl_multi_getcontent($multiHandles[$key]);
curl_multi_remove_handle($mh, $multiHandles[$key]);
}
echo $response_2;
curl_multi_close($mh);
?>
我没有机会测试这个,所以可能有些事情已经关闭,但希望你能得到我在这里所做的一切。希望这会有所帮助:)