Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
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.

770 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