情况
我正在现有的网站中创建一个功能。使用此功能,您可以从Google导入联系人。无论如何..我的老板告诉我,我必须使用任务队列从api获取联系人。这是因为有些人有超过9000(双关语)联系人,当这些人试图导入他们的联系人时,它会变得非常慢。
问题
如何使用任务队列获取联系人并将这些1逐个添加到数组中。
码
$return = array();
if (!empty($contacts['feed']['entry'])) {
foreach($contacts['feed']['entry'] as $contact) {
//retrieve Name and email address
$exploded = explode("/", $contact['id']['$t']);
$return[] = array (
'firstname'=> $contact['gd$name']['gd$givenName']['$t'],
'lastname'=> $contact['gd$name']['gd$familyName']['$t'],
'email' => $contact['gd$email'][0]['address'],
'phoneNumber' => $contact['gd$phoneNumber'][0]['$t'],
'city' => $contact['gd$structuredPostalAddress'][0]['gd$city']['$t'],
'street' => $contact['gd$structuredPostalAddress'][0]['gd$street']['$t'],
'country' => $contact['gd$structuredPostalAddress'][0]['gd$country']['$t'],
'birthday' => $contact['gContact$birthday']['when'],
'id' => end($exploded),
);
}
}
$google_contacts = $return;
unset($_SESSION['google_code']);
}
答案 0 :(得分:1)
关于任务队列要记住的第一件事是它们独立于调用它们的请求执行,所以你不能等待它们完成并将该数组返回给用户 - 任务需要将它存储在某个地方或者用它做点什么。
start_import.php:
$task = new PushTask('/do_import', $contacts);
$task->add();
echo("Started import job!");
do_import.php:
if (!empty($contacts['feed']['entry'])) {
foreach($contacts['feed']['entry'] as $contact) {
//retrieve Name and email address
$exploded = explode("/", $contact['id']['$t']);
$return[] = array (
'firstname'=> $contact['gd$name']['gd$givenName']['$t'],
'lastname'=> $contact['gd$name']['gd$familyName']['$t'],
'email' => $contact['gd$email'][0]['address'],
'phoneNumber' => $contact['gd$phoneNumber'][0]['$t'],
'city' => $contact['gd$structuredPostalAddress'][0]['gd$city']['$t'],
'street' => $contact['gd$structuredPostalAddress'][0]['gd$street']['$t'],
'country' => $contact['gd$structuredPostalAddress'][0]['gd$country']['$t'],
'birthday' => $contact['gContact$birthday']['when'],
'id' => end($exploded),
);
}
}
// Do something with $return, like persist it to the Datastore or Cloud Storage...
app.yaml处理程序示例:
handlers:
- url: /do_import
script: do_import.php
- url: /start_import
script: start_import.php
当然,阅读文档有助于了解更多信息:
在PHP中使用推送队列 https://cloud.google.com/appengine/docs/php/taskqueue/overview-push