J
J
jirniy2014-03-25 09:37:55
Zend Framework
jirniy, 2014-03-25 09:37:55

Ways to collect additional links from Yandex.Direct?

Good morning, Toaster.
One problem arose with the parsing of additional links that are placed under the ads that are displayed in Yandex. The main links are taken and saved normally, but the advertising department of our company decided that they also need to save additional links that are under these ads, and this is where the problem arose.
It looks like this - the one when the system that I got is absolutely without documentation and there is no access to the developers either, you can’t refuse and take something else,
and digging in the code led to this piece of code:

public function importYandexDirect($account) {
        $login  = $account->login;
        $appid  = $account->application_id;
        $token  = $account->token;

        $stream  = $account->fetchStream();
        $service = $account->fetchService();
        $user    = $stream->fetchUser();

        $wsdlurl = 'https://soap.direct.yandex.ru/wsdl/v4/';
        $client = new SoapClient($wsdlurl, array(
            'trace'          => 1,
            'exceptions'     => 1,
            )
        );

        $headers = array();
        $headers[] = new SoapHeader($wsdlurl, 'login', $login);
        $headers[] = new SoapHeader($wsdlurl, 'application_id', $appid);
        $headers[] = new SoapHeader($wsdlurl, 'token', $token);
        $headers[] = new SoapHeader($wsdlurl, 'locale', 'ru');

        $client->__setSoapHeaders($headers);
        if ($account->sub_account) {
            $sas = explode(',', $account->sub_account);
        } else {
            $sas = array('' => '');
        }

        foreach ($sas as $sa) {
            try {
                if ($sa) {
                    $campaigns = $client->GetCampaignsList(array($sa));
                } else {
                    $campaigns = $client->GetCampaignsList();
                }
            } catch (Exception $e) {
                echo "auth: $login failed\n";
                return false;
            }

 $cids = array();
            if ($account->campaign_id || $service) {
                foreach ($campaigns as $row) {
                    if ((! $account->campaign_id && $row->IsActive == 'Yes') || $account->campaign_id == $row- >CampaignID) {
                        $cids[] = $row->CampaignID;
                    }
                }
            } else {
                foreach ($campaigns as $row) {
                    $campaign = Wt_Factory::getInstance('Campaigns')->fetchRow(array('remote_id = ?' => $row- >CampaignID, 'user_id = ?' => $user->i$
                    if ($row->IsActive == 'Yes') {
                        $cids[] = $row->CampaignID;
                    }
                }
            }

            $params = array(
                'CampaignIDS' => $cids,
                'GetPhrases'  => 'WithPrices'
            );


            $adverts = array();

            if (! empty($cids)) {

 try {
                    $adverts = $client->GetBanners($params);
                } catch (Exception $e) {
                    "get banners: $login" . $e->getMessage() . "\n";
                    continue;
                }
            }

            $toUpdate = array();
            foreach ($adverts as $row) {
                if ($row->StatusArchive == 'Yes') continue;


                $campaign = Wt_Factory::getInstance('Campaigns')->fetchRow(array('remote_id = ?' => $row->CampaignID, 'user_id = ?' => $user->id));
                if ($service) {
                    $serviceId = $service->id;
                } elseif ($campaign) {
                    $serviceId = $campaign->fetchService()->id;
                } else {
                    continue;
                }


 $data = array(
                    'title'         => $row->Title,
                    'text'          => $row->Text,
                    'external_link' => 'http://' . $row->Href,
                    'service_id'    => $serviceId,
                    'user_id'       => $user->id,
                    'type'          => 'yandex_direct',
                    'remote_id'     => $row->BannerID,
                    'campaign_id'   => $campaign ? $campaign->id : null,
                );

                $item = $this->fetchRow(array('remote_id = ?' => $row->BannerID));
                if ($item) $data['id'] = $item->id;

                $id = $this->save($data);


     if (strpos($row->Href, 'aid=') === false) {
                    if (! $item) {
                        $item = $this->findById($id);
                    }

                    if (strpos($row->Href, '?') === false) {
                        $sep = '?';
                    } else {
                        $sep = '&';
                    }

                    $url = $row->Href . $sep . "aid={$item->id}&stid={$stream->id}&src={keyword}";

                    $row->Href = $url;
                    $item->external_link = 'http://' . $url;

                    $toUpdate[$row->BannerID] = $row;
                    $item->save();
                }


 if (! empty($row->Sitelinks)) {
                    $changed = false;
                    foreach ($row->Sitelinks as &$link) {
                        if (strpos($link->Href, 'aid=') === false) {
                            if (! $item) {
                                $item = $this->findById($id);
                            }

                            if (strpos($link->Href, '?') === false) {
                                $sep = '?';
                            } else {
                                $sep = '&';
                            }



                         $link->Href = $link->Href . $sep . "aid={$item->id}&stid={$stream->id}&src={keyword}";
                            $changed = true;
                        }
                    }

                    if ($changed) {
                        $toUpdate[$row->BannerID] = $row;
                    }
                }
            }

            if (count($toUpdate)) {
                try {
                    $res = $client->CreateOrUpdateBanners($toUpdate);
                } catch (Exception $e) {
                    echo "update: $login " . $e->getMessage() . "\n";
                }
            }
        }

        return count($this->errors) ? false : true;
    }



    public function archive($ids) {
        if (! is_array($ids)) {
            $ids = array($ids);
        }

        return $this->update(array('is_hidden' => 't'), array('id IN (?)' => $ids));
    }

    public function pickArticle($id, $stid) {
        $advert = $this->findById($id);
        $stream = Wt_Factory::getInstance('Streams')->findById($stid);

        $data = array();
        $data['title']     = $advert->getTitle();
        $data['stream_id'] = $stream->id;
        $data['user_id']   = $stream->user_id;
        $data['type']      = 'article';

        $rotation = array(
            array('advert_id' => $advert->id)
        );

        return Wt_Factory::getInstance('Blocks')->save($data, array(), $rotation);
    }
}

Since I myself code more in C#\C++, it’s difficult to deal with PHP code right away
(three months of experience with this language in total), and the authorities demand that I turn in the task by tomorrow evening, hinting that there is nothing complicated, and I For a week now I've been trying to figure out what was written by previous progers, who suddenly fell down. As we managed to find out, it is precisely these additional links that are cut off somewhere in the code, and now I ask, maybe someone smart in the topic will tell you what to correct. Many thanks to all who responded.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question