I working with one of email sender services API and facing a problem. Here is my process:
1) I'm fetching all subscribers emails (more than 1000 records),
2) Then I need to get XML info from API for the EACH email,
3) And then I need to save this info to the DB.
I use Yii2 console command but after several minutes the script terminates with 'Error while sending QUERY packet.'
How can I resolve this issue?
Yii console command (action):
class StartAction extends Action{
public function run(){
$uniqEmails = $activitiesComp->getUniqueEmails(); // emails array
$feedHelper = new FeedHelper([
'apiKey' => $value,
'apiHost' => $key,
'date' => $this->date,
'columns' => 'Extended',
]);
$XMLSubscriberArr = [];
foreach ($uniqEmails as $email) {
$XMLSubscriberArr[] = $feedHelper->getSubscriberInfo($email);
}
foreach ($XMLSubscriberArr as $XMLSubscriberInfo) {
$parsedSubscriber = new SimpleXMLElement($XMLSubscriberInfo);
$subscriberEmail = (string) $parsedSubscriber->Data->Email;
if (!$subscribersModel = $subscribersComp->getSubscriberByEmail($subscriberEmail)) {
$subscribersModel = $subscribersComp->getModel();
}
$subscribersModel->ip = (string)$parsedSubscriber->Data->Ip;
$subscribersModel->email = $subscriberEmail;
$subscribersModel->first_name = (string) $parsedSubscriber->Data->Firstname;
$subscribersModel->second_name = (string) $parsedSubscriber->Data->Lastname;
$properties = $parsedSubscriber->Data->Properties;
foreach ($properties->Property as $property) {
if ($property->Name == "custom_id") {
$subscribersModel->sem_id = (int) $property->StringValue;
}
}
$subscribersModel->save();
}
echo 'Success';
return ExitCode::OK;
}
}
Feed Helper class:
class FeedHelper
{
public $apiKey;
public $apiHost;
public $date;
public $columns;
public function __construct($params)
{
$this->apiKey = $params['apiKey'];
$this->apiHost = $params['apiHost'];
$this->date = $params['date'];
$this->columns = $params['columns'];
}
public function getSubscriberInfo($email)
{
$params = [
'apiKey' => $this->apiKey,
'email' => $email,
'option' => 'full',
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->apiHost . 'Api/Subscribers/?' . http_build_query($params));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
}