Solved

How do I add For Each to this PHP/cURL and run through pagination

Posted on 2015-01-09
5
226 Views
Last Modified: 2015-01-10
This script actually works except the pages are not changing, the script just keeps posting the same page over and over.  ( page=$i )


for ($i = 1; $i <= 1000; $i++) {
     
    /* RUN CODE AND SCRAPE STUFF IN HERE */
 $url2 = 'http://www.website.com/inventory/catalog.php?classID=&dept=&type=&keywords=&brandID=&price1=&price2=&age=&orderBy=BA&page=$i';
    

	$ch = curl_init(); // initialize curl handle
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_VERBOSE, 1);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
	curl_setopt($ch, CURLOPT_AUTOREFERER, false);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,7);
	curl_setopt($ch, CURLOPT_REFERER, 'Referer: http://www.website.com/inventory/search.php');
	curl_setopt($ch, CURLOPT_URL,$url2); // set url to post to
	curl_setopt($ch, CURLOPT_FAILONERROR, 0);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
	
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
	curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
	curl_setopt($ch, CURLOPT_TIMEOUT, 50); // times out after 50s

	
	
	$buffer2 = curl_exec($ch); // run the whole process
	echo $buffer2;
	if(curl_exec($ch) === false)
	{
	    echo 'Curl error: ' . curl_error($ch);
	    exit;
	}
}

Open in new window

0
Comment
Question by:lawrence_dev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 40541670
"page=$i" is the  only thing changing.  You probably need some of the other values in the query string to be set to make it work.  Since they are not, you are probably getting nothing but the 'default' value or page.  Frequently, 'page' will refer to to page of a specific category.  You have not category.
0
 

Author Comment

by:lawrence_dev
ID: 40542117
If I run the same URL and change to page=2 or page=3, etc, I get different pages every time which is what I want.  The URL I have shown is a search of all of the products.  

All I am looking for is the page number to change every time a page completes.

.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40542145
That's confusing because that is the only thing that is changing the code you posted above.
0
 

Author Comment

by:lawrence_dev
ID: 40542152
I got it working.  Thanks for your help!  page='.$i;  instead of  page=$i.

$url2 = 'http://www.website.com/inventory/catalog.php?classID=&dept=&type=&keywords=&brandID=&price1=&price2=&age=&orderBy=BA&page='.$i;
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40542166
Oh, good!!
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question