Juan.Queiroz
Juan.Queiroz

Reputation: 227

Curl request method inside foreach loop got a error

I'm getting an error "Undefined offset: 1" when i use foreach (option 1) instead of duplicate code (option 2). With foreach, i'm using an array only with 2 strings to test. The error is on line $html_tt = get($direct_link_tt[1]);. Should i do something different in get method?

$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();

What i'm doing wrong?

Upvotes: 0

Views: 258

Answers (1)

Rajdip Chauhan
Rajdip Chauhan

Reputation: 345

I think you need to initialize both curl request with two different variables.

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;
}

I hope this helps you.

Upvotes: 1

Related Questions