Метод запроса curl внутри foreach l oop получил ошибку - PullRequest
0 голосов
/ 06 марта 2020

Я получаю сообщение об ошибке «Неопределенное смещение: 1», когда я использую foreach (вариант 1) вместо дублирующего кода (вариант 2). С foreach я использую массив только с 2 строками для тестирования. Ошибка в строке $html_tt = get($direct_link_tt[1]);. Должен ли я сделать что-то другое в методе get?

$cookie_path = dirname(__FILE__).'/cookie.txt';

function login($user, $pass, $account) {
  $URL = 'https://example.com/index.php';
  global $cookie_path;
  $ch = curl_init($URL);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, 'url_login='.$user.'&url_password='.$pass.'&url_account='.$account);
  curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
  curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_path);
  $ret = curl_exec($ch);
  curl_close($ch);

  return $ret;
}

function get($url) {
  global $cookie_path;
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
  curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_path);
  $html = curl_exec($ch);
  curl_close($ch);

  return $html;
}

function main() {
    $user = 'XXXXXX';
    $pass = 'XXXXXX';
    $account = 'XXXXX';
   $all_tt = getVals();

    $dataLogin = login($user, $pass, $account);

  preg_match('/"PHPSESSID", "(.*)"/U', $dataLogin, $phpSessionEV);
  preg_match('/uniqid=([0-9]*)&/U', $dataLogin, $uniqid);

//option 1 start
    foreach ($all_tt as $tt){
      $url_bef_ticket = "https://example.com/ajax_widget.php?session_id=".$phpSessionEV[1]."&uniqid=".$uniqid[1]."&lng=PO&theme={C9F11D78-448E-4EAC-A766-C904ED98458E}&widgetname=InternalFilteredQuery.InternalFilteredQueryWidget&widgetid=Grid_78A562B7_B6EA_45BF_9223_A2D42C74851F&WidgetParams=action%3DgetContent%26blockGUID%3D%257BC99ECD05-3D48-4C62-ABF0-66292053AED6%257D%26eventName%3DRequestFullfimentItem%26filterGUID%3D%257B096C2D42-0DF7-4A54-9978-D13E59BAB8ED%257D%26internalGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26title%3DPedidos%2Bde%2BServi%25C3%25A7o%26maxLines%3D5%26queryGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26reportGUID%3D%257BC910B0A9-D202-42D4-8E75-CC48FC3EA1D8%257D%26searchText%3D".$tt."%26searchGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26themeGUID%3D%257BC9F11D78-448E-4EAC-A766-C904ED98458E%257D";
      $html_bef_ticket = get($url_bef_ticket);
      preg_match('/<a class="aImg" href="(.*)"/U', $html_bef_ticket, $direct_link_tt);

      $html_tt = get($direct_link_tt[1]);
      print_r($html_tt);
    }
//option 1 end

//option 2 start
    $url_bef_t = "https://example.com/ajax_widget.php?session_id=".$phpSessionEV[1]."&uniqid=".$uniqid[1]."&lng=PO&theme={C9F11D78-448E-4EAC-A766-C904ED98458E}&widgetname=InternalFilteredQuery.InternalFilteredQueryWidget&widgetid=Grid_78A562B7_B6EA_45BF_9223_A2D42C74851F&WidgetParams=action%3DgetContent%26blockGUID%3D%257BC99ECD05-3D48-4C62-ABF0-66292053AED6%257D%26eventName%3DRequestFullfimentItem%26filterGUID%3D%257B096C2D42-0DF7-4A54-9978-D13E59BAB8ED%257D%26internalGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26title%3DPedidos%2Bde%2BServi%25C3%25A7o%26maxLines%3D5%26queryGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26reportGUID%3D%257BC910B0A9-D202-42D4-8E75-CC48FC3EA1D8%257D%26searchText%3DS1234567%26searchGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26themeGUID%3D%257BC9F11D78-448E-4EAC-A766-C904ED98458E%257D";
    $html_bef_ticket = get($url_bef_t);
    preg_match('/<a class="aImg" href="(.*)"/U', $html_bef_ticket, $direct_link_tt);

    $html_tt = get($direct_link_tt[1]);
    print_r($html_tt);

    $url_bef_t = "https://example.com/ajax_widget.php?session_id=".$phpSessionEV[1]."&uniqid=".$uniqid[1]."&lng=PO&theme={C9F11D78-448E-4EAC-A766-C904ED98458E}&widgetname=InternalFilteredQuery.InternalFilteredQueryWidget&widgetid=Grid_78A562B7_B6EA_45BF_9223_A2D42C74851F&WidgetParams=action%3DgetContent%26blockGUID%3D%257BC99ECD05-3D48-4C62-ABF0-66292053AED6%257D%26eventName%3DRequestFullfimentItem%26filterGUID%3D%257B096C2D42-0DF7-4A54-9978-D13E59BAB8ED%257D%26internalGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26title%3DPedidos%2Bde%2BServi%25C3%25A7o%26maxLines%3D5%26queryGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26reportGUID%3D%257BC910B0A9-D202-42D4-8E75-CC48FC3EA1D8%257D%26searchText%3DS7654321%26searchGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26themeGUID%3D%257BC9F11D78-448E-4EAC-A766-C904ED98458E%257D";
    $html_bef_ticket = get($url_bef_t);
    preg_match('/<a class="aImg" href="(.*)"/U', $html_bef_ticket, $direct_link_tt);

    $html_tt = get($direct_link_tt[1]);
    print_r($html_tt);
//option 2 end

}

main();

Что я делаю не так?

1 Ответ

1 голос
/ 06 марта 2020

Я думаю, вам нужно инициализировать оба запроса curl двумя разными переменными.

function login($user, $pass, $account) {
  $URL = 'https://example.com/index.php';
  global $cookie_path;
  $ch = curl_init($URL);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, 'url_login='.$user.'&url_password='.$pass.'&url_account='.$account);
  curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
  curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_path);
  $ret = curl_exec($ch);
  curl_close($ch);

  return $ret;
}

function get($url) {
  global $cookie_path;
  $ch2 = curl_init();
  curl_setopt($ch2, CURLOPT_URL, $url);
  curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch2, CURLOPT_CONNECTTIMEOUT, 10);
  curl_setopt($ch2, CURLOPT_COOKIEJAR, $cookie_path);
  curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie_path);
  $html = curl_exec($ch2);
  curl_close($ch2);

  return $html;
}

Надеюсь, это вам поможет.

...