M
M
Max Sh2020-04-13 20:33:18
PHP
Max Sh, 2020-04-13 20:33:18

How to pass data one by one to a PHP form?

Hello! I am making a script for myself for mass replenishment of phone numbers via the PAYEER API and got stuck on this question (just php is not mine, just for fun and for myself)

How to make it so that I can drop a bunch of numbers at once and they are somehow distributed and sent in the form? I want to make it so that I throw a pack of numbers and they are automatically replenished.

Code for what I already did:

form.php
<? top('Пополнить номера') ?>
<div class="padding">
  <form action="/form" method="post">
    <select name="operator">
        <option value="24899291">МТС</option>
        <option value="24899391">Мегафон</option>
        <option value="95877310">Теле2</option>
        </select>
    <p><input type="text" name="phone" placeholder="Номер телефона"></p>
    <p><input type="submit" name="enter" value="Пополнить"></p>
  </form>
</div>
<? bottom() ?>

home.php
<?
if ($_POST['enter'])
{
    require_once ('cpayeer.php');
    $accountNumber = 'P1XXXXXXX';
    $apiId = '00000000';
    $apiKey = 'xxxxxxxxxx';
    $payeer = new CPayeer($accountNumber, $apiId, $apiKey);
    if ($payeer->isAuth())
    {
        $initOutput = $payeer->initOutput(array(
            'ps' => $_POST['operator'],
            'curIn' => 'RUB',
            'sumOut' => 10,
            'curOut' => 'RUB',
            'param_ACCOUNT_NUMBER' => $_POST['phone'],
        ));

        if ($initOutput)
        {
            $historyId = $payeer->output();
            if ($historyId > 0)
            {
                echo "Выплата успешна";
            }
            else
            {
                echo '<pre>' . print_r($payeer->getErrors() , true) . '</pre>';
            }
        }
        else
        {
            echo '<pre>' . print_r($payeer->getErrors() , true) . '</pre>';
        }
    }
    else
    {
        echo '<pre>' . print_r($payeer->getErrors() , true) . '</pre>';
    }
}
?>

PAYEER API PHP class

<?php
class CPayeer
{
  private $url = 'https://payeer.com/ajax/api/api.php';
  private $agent = 'Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0';

  private $auth = array();

  private $output;
  private $errors;
  private $language = 'ru';

  public function __construct($account, $apiId, $apiPass)
  {
    $arr = array(
      'account' => $account,
      'apiId' => $apiId,
      'apiPass' => $apiPass,
    );

    $response = $this->getResponse($arr);

    if ($response['auth_error'] == '0')
    {
      $this->auth = $arr;
    }
  }

  public function isAuth()
  {
    if (!empty($this->auth)) return true;
    return false;
  }

  private function getResponse($arPost)
  {
    if (!function_exists('curl_init'))
    {
       die('curl library not installed');
       return false;
    }

    if ($this->isAuth())
    {
      $arPost = array_merge($arPost, $this->auth);
    }

    $data = array();
    foreach ($arPost as $k => $v)
    {
      $data[] = urlencode($k) . '=' . urlencode($v);
    }
    $data[] = 'language=' . $this->language;
    $data = implode('&', $data);

    $handler  = curl_init();
    curl_setopt($handler, CURLOPT_URL, $this->url);
    curl_setopt($handler, CURLOPT_HEADER, 0);
    curl_setopt($handler, CURLOPT_POST, true);
    curl_setopt($handler, CURLOPT_POSTFIELDS, $data);
    curl_setopt($handler, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($handler, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($handler, CURLOPT_USERAGENT, $this->agent);
    curl_setopt($handler, CURLOPT_RETURNTRANSFER, 1);

    $content = curl_exec($handler);
    //print_r($content);

    $arRequest = curl_getinfo($handler);
    //print_r($arRequest);

    curl_close($handler);
    //print_r($content);

    $content = json_decode($content, true);

    if (isset($content['errors']) && !empty($content['errors']))
    {
      $this->errors = $content['errors'];
    }

    return $content;
  }

  public function getPaySystems()
  {
    $arPost = array(
      'action' => 'getPaySystems',
    );

    $response = $this->getResponse($arPost);

    return $response;
  }

  public function initOutput($arr)
  {
    $arPost = $arr;
    $arPost['action'] = 'initOutput';

    $response = $this->getResponse($arPost);

    if (empty($response['errors']))
    {
      $this->output = $arr;
      return true;
    }

    return false;
  }

  public function output()
  {
    $arPost = $this->output;
    $arPost['action'] = 'output';

    $response = $this->getResponse($arPost);

    if (empty($response['errors']))
    {
      return $response['historyId'];
    }

    return false;
  }

  public function getHistoryInfo($historyId)
  {
    $arPost = array(
      'action' => 'historyInfo',
      'historyId' => $historyId
    );

    $response = $this->getResponse($arPost);

    return $response;
  }

  public function getBalance()
  {
    $arPost = array(
      'action' => 'balance',
    );

    $response = $this->getResponse($arPost);

    return $response;
  }

  public function getErrors()
  {
    return $this->errors;
  }

  public function transfer($arPost)
  {
    $arPost['action'] = 'transfer';

    $response = $this->getResponse($arPost);

    return $response;
  }

  public function SetLang($language)
  {
    $this->language = $language;
    return $this;
  }

  public function getShopOrderInfo($arPost)
  {
    $arPost['action'] = 'shopOrderInfo';

    $response = $this->getResponse($arPost);

    return $response;
  }

  public function checkUser($arPost)
  {
    $arPost['action'] = 'checkUser';

    $response = $this->getResponse($arPost);

    if (empty($response['errors']))
    {
      return true;
    }

    return false;
  }

  public function getExchangeRate($arPost)
  {
    $arPost['action'] = 'getExchangeRate';

    $response = $this->getResponse($arPost);

    return $response;
  }

  public function merchant($arPost)
  {
    $arPost['action'] = 'merchant';

    $arPost['shop'] = json_encode($arPost['shop']);
    $arPost['form'] = json_encode($arPost['form']);
    $arPost['ps'] = json_encode($arPost['ps']);

    if (empty($arPost['ip'])) $arPost['ip'] = $_SERVER['REMOTE_ADDR'];

    $response = $this->getResponse($arPost);

    if (empty($response['errors']))
    {
      return $response;
    }

    return false;
  }
}
?>


This is what the frontend looks like now

5e94a1e38e11b893473771.png

I apologize in advance if I phrased the question incorrectly. Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valera Karmanov, 2020-04-13
@motokraft

1. option (if the replenishment amount for all numbers is the same) - in the input form with the name phone you make a textarea, and you write each number from a new line, then in the handler you break the value of the textarea into \n (line break)
and as a result it turns out that all the numbers be contained in an array and then work with that array. For example

<textarea name="phone">24899291
24899391
95877310</textarea>

so i get an array
$phones = array('24899291', '24899391', '95877310');

then I work with this array
foreach($phones as $phone)
{
  $initOutput = $payeer->initOutput(array(
    'ps' => $_POST['operator'],
    'curIn' => 'RUB',
    'sumOut' => 10,
    'curOut' => 'RUB',
    'param_ACCOUNT_NUMBER' => $phone,
  ));

  if ($initOutput)
  {
    $historyId = $payeer->output();
    if ($historyId > 0)
    {
      echo "Выплата успешна";
    }
    else
    {
      echo '<pre>' . print_r($payeer->getErrors() , true) . '</pre>';
    }
  }
  else
  {
    echo '<pre>' . print_r($payeer->getErrors() , true) . '</pre>';
  }
}

2. option (if each number has its own replenishment amount) - you can issue it as you like, but this does not change the meaning. Option for one phone. Form code
<p><input type="text" name="phone[0][number]" placeholder="Номер телефона"></p>
<p><input type="text" name="phone[0][sum]" placeholder="Сумма пополнения"></p>

the handler will receive the following data (example for three phones)
array(
  array(
    'number' => '24899291',
    'sum' => '10'
  ),
  array(
    'number' => '24899391',
    'sum' => '20'
  ),
  array(
    'number' => '95877310',
    'sum' => '30'
  )
)

such field names are not random, because it is very convenient to work with them in the handler. Further, almost everything is the same as above
foreach($_POST['phones'] as $phone)
{
  $initOutput = $payeer->initOutput(array(
    'ps' => $_POST['operator'],
    'curIn' => 'RUB',
    'sumOut' => $phone['sum'],
    'curOut' => 'RUB',
    'param_ACCOUNT_NUMBER' => $phone['number'],
  ));

  if ($initOutput)
  {
    $historyId = $payeer->output();
    if ($historyId > 0)
    {
      echo "Выплата успешна";
    }
    else
    {
      echo '<pre>' . print_r($payeer->getErrors() , true) . '</pre>';
    }
  }
  else
  {
    echo '<pre>' . print_r($payeer->getErrors() , true) . '</pre>';
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question