foreach循环内的卷曲请求方法出错

问题描述 投票:0回答:1

当我使用foreach(选项1)而不是重复代码(选项2)时,出现错误“未定义偏移量:1”。对于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();

我做错了什么?

php curl request php-curl
1个回答
0
投票

我认为您需要使用两个不同的变量来初始化两个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;
}

希望对您有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.