Skip to content
Advertisement

PHP cURL login and loop through URLs stuck on first URL

I am trying to loop through part numbers to get their associated information. If I echo the part number it is always the same. even though the starting part number is correct. Ideas?

include('./simple_html_dom.php');
require './includes/config.inc.php';
require './includes/Database.inc.php';


$db = new Database();

$db->query('SELECT part_num FROM GE LIMIT 4');
$results = $db->resultset();

foreach($results as $result){

$startingPN = trim($result['part_num']);

$curlPost['accountNumber']="*****";
$curlPost['password']="******";
$curlPost['contactForm']="https://www.*****.com/images/layout/input/submit.gif";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL , "https://www.*****.com/login");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
$response = curl_exec ($ch);
curl_setopt($ch, CURLOPT_URL, 'https://www.******.com/search/?part='.$startingPN); // set url for next request

$exec = curl_exec($ch);


// Create a DOM object
 $html = new simple_html_dom();
 // Load HTML from a string
 $html->load($exec);


    foreach($html->find('table.searchWholesale') as $article) {

        $item['part_num'] = $article->find('span.purpleBold', 0)->plaintext;
        $item['description'] = $article->find('span.twelve', 1)->plaintext;
        $item['cost'] = $article->find('span.fourteen', 1)->plaintext;
        $item['retail'] = $article->find('span.fourteen', 2)->plaintext;
        $item['brand'] = $article->find('td.leftColumn',0);
        $item['count'] = count($article->find('div.productHide'));
        $item['productHide1'] = $article->find('div.productHide', 0)->plaintext;
        $item['productHide2'] = $article->find('div.productHide', 1)->plaintext;

        $articles[] = $item;
    }
    
    $last_checked = date("U");
    $part_num = $articles[0]['part_num'];
    
   
        $title = ucwords(strtolower($articles[0]['description']));
        $cost = floatval(ltrim($articles[0]['cost'], '$'));
        $retail = floatval(ltrim($articles[0]['retail'], '$'));
        $brand = $articles[0]['brand'];
        $brand->children(0)->outertext = "";
        $brand->children(1)->outertext = "";
        $brand->children(2)->outertext = "";
        $brand->children(3)->outertext = "";
        $brand->children(4)->outertext = "";
        $brand->children(5)->outertext = "";
        $brand->children(6)->outertext = "";
        //$brand->children(7)->outertext = "";
        //$brand->children(8)->outertext = "";
        //$brand->children(9)->outertext = "";

        $brand_qty = explode(" ", $brand);
        $name_brand =  $brand_qty[15];
        $qty_avail = $brand_qty[21];

        echo '<br>'.$startingPN . '<br>' . $part_num . '<br>' . $title . '<br>' . $cost . '<br>' . $retail  . '<br>' . $name_brand;
}

This outputs… The first one is the only one that is correct. NOTICE WE04X10010 is being echoed as the part number in all subsequent loops.

WE04X10010
WE04X10010
Buzzer
55.5
103.58
General

WD12X441 <-($startingPN)
WE04X10010 <- THIS SHOULD BE THE SAME AS $startingPN (WD12X44)
Buzzer
55.5
103.58
General

WH11X139 <-($startingPN)
WE04X10010 <- THIS SHOULD BE THE SAME AS $startingPN (WH11X139)
Buzzer
55.5
103.58
General

WH11X10015 <-($startingPN)
WE04X10010 <- THIS SHOULD BE THE SAME AS $startingPN (WH11X10015)
Buzzer
55.5
103.58
General

Advertisement

Answer

I think your $articles variable need to reset in each master loop. Like below:

$articles = [];
foreach($html->find('table.searchWholesale') as $article) {

        $item['part_num'] = $article->find('span.purpleBold', 0)->plaintext;
        $item['description'] = $article->find('span.twelve', 1)->plaintext;
        $item['cost'] = $article->find('span.fourteen', 1)->plaintext;
        $item['retail'] = $article->find('span.fourteen', 2)->plaintext;
        $item['brand'] = $article->find('td.leftColumn',0);
        $item['count'] = count($article->find('div.productHide'));
        $item['productHide1'] = $article->find('div.productHide', 0)->plaintext;
        $item['productHide2'] = $article->find('div.productHide', 1)->plaintext;

        $articles[] = $item;
    }
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement