Solved

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

Posted on 2015-01-09
5
182 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now