Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using cURL in a foreach-loop (php)

I have many HTTPS-URLS where I have to find a Special Phrase, so I´m trying to use cURL in a foreach Loop, but it´s not working.

...
foreach($sites as $site) {

    $URL = $site;        

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

    $response = curl_exec($ch);
    curl_close($ch);

}

print substr($response, $start, $i);
...

If I use just a single HTTPS-URL I get the Phrase, but inside a foreach-loop it´s not working. Can someone help me? (:

Please excuse my poor english..

like image 339
Dex Ter Avatar asked Oct 18 '25 14:10

Dex Ter


1 Answers

this may help :) store result inside an array

$sites = ['https://stackoverflow.com','http://example.org'];
$result = [];
foreach ($sites as $site) {

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $site);

    // User agent
    curl_setopt($ch, CURLOPT_USERAGENT, "PHP Curl");

    // Include header in result? (0 = yes, 1 = no)
    curl_setopt($ch, CURLOPT_HEADER, 0);

    // Should cURL return or print out the data? (true = return, false = print)
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Timeout in seconds
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);

    // execute the given URL, and return output
    $result[] = curl_exec($ch);

    curl_close($ch);


}

var_dump($result);
like image 177
azjezz Avatar answered Oct 21 '25 02:10

azjezz