Reputation: 35
I want to fetch multiple URLs dynamically using cURL and PHP. When I try this with a single URL it works, but doesn't for multiple URLs. Please help.
I am using a form to send URLs:
$urls = $_POST["urls"];
require_once('simple_html_dom.php');
$useragent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
foreach ($urls as $url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
$str = curl_exec($curl);
curl_close($curl);
}
$html= str_get_html($str);
foreach($html->find('span.price') as $e)
echo $e->innertext . '<br>';
Upvotes: 0
Views: 620
Reputation: 2051
The issue is that in your form, you have text inputs with the same name. When it gets passed to curl.php, it's not passed as an array. You can fix this by doing the following in your form:
<form action="curl.php">
<input type="text" name="urls[]" />
<input type="text" name="urls[]" />
...
</form>
Now, urls will be passed in as an array, and the foreach call will work normally.
Upvotes: 1