S
S
santavits2018-04-07 12:04:00
API
santavits, 2018-04-07 12:04:00

Why doesn't api epn.bz work?

Good day, help solve the problem with api epn
What should the script do to parse posts from other aliexpress-themed publics, replace the link to the product and post to my group.
But when replacing the link, the script is simply endlessly loaded and does not create a link.

spoiler
Here is the parser.php script
<?php
ini_set("display_errors",1);
error_reporting(E_ALL); 


include 'EpnBz.php';
include 'Client.php';
include 'BadAuthException.php';
include 'Auth.php';


function create_new_epn_link($epn_username,$epn_password,$text){
  
$text = str_replace('<br>','', $text);
//Поиск целевой строки
preg_match_all("/((ht f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/",  $text, $matches);
echo '<pre>';
print_r($matches); 
$old_short_url = $matches[0][0];
if (!empty($old_short_url)){
  $url = full_url(trim($old_short_url));
  while (substr_count($url, 'https://ru.aliexpress.com/') != 1){
    $url = full_url(trim($url));
  }
  $url = strstr($url,'html?', true).'html';

  //Раота с EPN
  $epn = new \EpnBzLib\EpnBz($epn_username, $epn_password); //создание объекта и авторизация на сайте

  $promo_url = $epn->getUrl($url); //получени промо url
  $short_url = $epn->short($promo_url); //сокращение промо урла к виду ali.pub/ABCDE `
  $short_url = ' '.$short_url;

  $text= str_replace(trim($old_short_url),$short_url,$text);
}

return $text;
}

function full_url($url){
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  $a = curl_exec($ch);
  curl_close( $ch ); 
   
  $headers = explode("\n",$a);
   
  $redir = $url;
   
  $j = count($headers);
  for($i = 0; $i < $j; $i++){
       
  if(strpos($headers[$i],"Location:") !== false){
      $redir = trim(str_replace("Location:","",$headers[$i]));
      break;
    }
  }
   
  return $redir;
}

function send_vk_api ($method, $params){
  $get_params = http_build_query($params);

  $url = 'https://api.vk.com/method/'.$method.'?'. $get_params;
  $array_api = json_decode(file_get_contents($url)); 
  
  return $array_api;
}

function uploadposter ($token,$image_path){
 $data = file_get_contents("https://api.vk.com/method/photos.getWallUploadServer?group_id=164763840&v=5.74&access_token=".$token);
$data=json_decode($data);

$link=$data->response->upload_url;
 
$post_params = array ( 'photo' => new CURLFile( $image_path, 'image/jpeg', 'test.jpg'));
 $ch = curl_init($link);
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
 $response = curl_exec( $ch );
 curl_close( $ch );
 

 
$response=json_decode($response);
echo '<pre>';
print_r($response);
$server=$response->server;
$photo=$response->photo;
$hash=$response->hash;
 echo $photo;
$link2= "https://api.vk.com/method/photos.saveWallPhoto?group_id=164763840&access_token=".$token."&server=".$server."&hash=".$hash."&photo=".$photo."v=5.74";
$data3 = file_get_contents($link2);
$data3 = json_decode($data3, true);


echo $image_path.'<pre>';
print_r($data3);
$eee= $data3[response][0][id];

return $eee;

}


function sendvkposter($owner_id , $images, $text, $token){
    $url = 'https://api.vk.com/method/wall.post';
    $params = array(
        'owner_id' => "-".$owner_id,    // Кому отправляем
    'attachments' =>  $images,
    'message' => $text,   // Что отправляем
        'access_token' => $token,  // access_token можно вбить хардкодом, если работа будет идти из под одного юзера
        'v' => '5.00'
    );

    // В $result вернется id отправленного сообщения
    $result = file_get_contents($url, false, stream_context_create(array(
        'http' => array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => http_build_query($params)
        )
    )));
  
  return $result;
}


$epn_username = file('epn.txt')[0];
$epn_password = file('epn.txt')[1];	
echo $epn_username.' '.$epn_password.' <br>';

$token = file('vk.txt')[0];
$group_id = file('vk.txt')[1];
echo $token.' '.$group_id;

$params = array(
        'owner_id' => "-39209729",
    'offset' => "1",
    'v' => "5.47",
        'access_token' => $token
    );
  
$array = send_vk_api ("wall.get", $params);
//echo '<pre>';
//print_r($array);

$post_type = $array->response->items[1]->post_type;
$ads = $array->response->items[1]->marked_as_ads;
$id = $array->response->items[1]->id;

$post_ids = file("ids.txt");
if (in_array($id,$post_ids)){
  echo "Эта запись уже есть";
}else{

  if (($post_type == "copy") or ($ads == 1)){
    echo 'Это рекламный пост';
  }else{$text = $array->response->items[1]->text;
    $text = create_new_epn_link($epn_username,$epn_password,$text);
    $photo_array = $array->response->items[1]->attachments;
    $count = count($photo_array);

    //echo '<pre>';
    //print_r($photo_array);

    for ($i=0; $i <$count; $i++){
      $photo = $photo_array[$i]->photo->src_xxbig;
      if(empty($photo)){
        $photo = $photo_array[$i]->photo->src_xbig;
        if(empty($photo)){
          $photo = $photo_array[$i]->photo->src_big;
          if(empty($photo)){
            $photo = $photo_array[$i]->photo->src;
          }
        }
      }
      
      
      $file = __DIR__."/"."image".$i.".jpg";
      file_put_contents($file, file_get_contents($photo));
      //watermark_image($file);
      
      $upload_image = uploadposter ($token, $file);
      //usleep(500000);
      
      if (empty($upload_image)){
        $photo_str = $photo_str."";
      }else{
        $photo_str = $photo_str.$upload_image.",";
      }
      //unlink($file);

    }
    
    //echo $photo_str;

    if (empty($photo_str)){
      echo "Не получилось скачать фотки.";
    }else{
      $post_ready = sendvkposter($group_id, $photo_str, $text, $token);  
      //echo '<pre>';
      //print_r ($post_ready);
    }
  }
    
  $fp = fopen(__DIR__ ."/ids.txt", "a");
  fwrite($fp, $id."\n");
  fclose($fp);	
}

?>

HERE is the EpnBz.php file script through which a new link is created
<?php namespace EpnBzLib;

class EpnBz
{

  public function __construct($epn_login, $epn_password)
  {
    $this->client = new Client();
    $auth = new Auth($this->client);
    if (!$auth->login($epn_login, $epn_password)) {
      throw new BadAuthException('error');
      return false;
    } else {
      $this->auth = true;
    }
  }

  public function getUrl($url, $name = false, $offer_type = 'ali')
  {
    if (!$name) {
      $name = rand(1, 4512312).rand(5342, 1231313);
    }
    $ali_page = $this->client->get($url);
    $ali_url = $this->client->getUrl();
    if (strpos($ali_url, 'alipromo') !== false) {
      $html = new \simple_html_dom();
      $html->load($ali_page);
      $iframes = $html->find(".main-iframe");
      foreach ($iframes as $iframe) {
        $u = $iframe->src;
        $d = explode('to=', urldecode($u), 2);
        if (array_key_exists(1, $d)) {
          return $this->getUrl($d[1], $name, $offer_type);
        } else {
          return false;
        }
      }
    } 

    $temp = json_encode(array("format" => "1","isAllow" => 0,"link" => $ali_url,"desc" => $name,"image" => "","rejectChange" => false,"expiration_time" => "","no_affiliate_direct" => true,"lang" => null,"selected_banners"=> array(),"size" => "300x250","offer_type" => $offer_type));
    $create_url = 'https://epn.bz/ru/creative/create';
    $res = $this->client->post($create_url, $temp, true);

    $list_url = 'https://epn.bz/ru/creative/list';
    $list_temp = '{"page":1,"pagebase":"#/creative/tab/list","description":""}';
    $result = json_decode($this->client->post($list_url, $list_temp), true);
    foreach ($result as $creatives) {
      foreach ($creatives as $creative) {
        if (!is_array($creative)) {
          continue;
        }
        if (!array_key_exists('description', $creative)) {
          continue;
        }
        if ($creative['description'] == $name) {
          return $creative['code'];
        }
      }
    }
    return false;
  }

  public function short($url)
  {
    $short_url = 'https://epn.bz/ru/creative/url-to-short';
    $data = json_encode(array('url' => $url));
    $return = trim($this->client->post($short_url, $data, true));
    if (strpos($return, 'ali.pub') === false) {
      return false;
    }
    return $return;
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vn38, 2019-02-10
@vn38

Have you solved the problem?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question