Jump to content

cURL and foreach in while


MadLittleMods

Recommended Posts

Well usually I can solve my problems after some grueling hours but this one just has me stumped.

 

A similar script worked just 10 days ago on my server. I had just tried it today and it isnt working and that is why i made a more streamlined version hoping it would work but it did the same exact thing.

 

I removed all the pregs and changed it to a more broad site that I know works with cURL. I made it a simpler script just for debugging and even this doesn't work. I noticed my host updated the cPanel and I think my php version is different and is now 5.2.15

 

All this script does is nothing i visit and the loading wheel just spins up on the tab and nothing is outputted.

 

Here is my updated script:

<?php



echo 'started up<br>';


do {

echo 'doing...<br>';

$url_to_curl = "http://visualpulse.net/forums/index.php";

// create a new curl resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url_to_curl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// grab URL, and return output
$output = curl_exec($ch);

// close curl resource, and free up system resources
curl_close($ch);

// echo $output;

echo 'just curled<br>';

// echo $output;


echo '<br><br>done curling: '.$url_to_curl.'<br><br>';

// find the people
preg_match_all('/<a class="subject board_title_in_info_div" href="(.*?)" name="(.*?)">(.*?)<\/a>/', $output, $creators);

//print_r($creators);


foreach ($creators[3] as $key => $creator) 
{
	echo $creator.'<br>';

}


} while(!empty($creators[3]));




?>

Link to comment
https://forums.phpfreaks.com/topic/243544-curl-and-foreach-in-while/
Share on other sites

Well after my friend said "it's becuase you are defining creators inside the loop", I knew exactly what I needed to do.

 

I was using $creators outside of its scope in the while. So i just made a variable called $done in the bigger scope and that worked great. Here is the code that works but if you remove the $done = 0 at the end then it doesn't work.

 

How can you put a curl in a while loop and make it work? I did this about 10 days ago but now it just doesn't work..

 

<?php



echo 'started up<br>';


$done = 1;

do {

echo 'doing...<br>';

$url_to_curl = "http://visualpulse.net/forums/index.php";

// create a new curl resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url_to_curl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// grab URL, and return output
$output = curl_exec($ch);

// close curl resource, and free up system resources
curl_close($ch);

// echo $output;

echo 'just curled<br>';

// echo $output;


echo '<br><br>done curling: '.$url_to_curl.'<br><br>';

// find the people
preg_match_all('/<a class="subject board_title_in_info_div" href="(.*?)" name="(.*?)">(.*?)<\/a>/', $output, $creators);

//print_r($creators);


foreach ($creators[3] as $key => $creator) 
{
	echo $creator.'<br>';

}


$done = 0;



} while($done);




?>

Since i can't just edit my existing post so my thread doesn't bump...

 

The script does work i just didn't wait long enough for all the pages to load. Before my script spit out info as it went but now it does it all in one chunk which I was not used to.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.