在foreach循环中使用cURL(php) [英] Using cURL in a foreach-loop (php)

查看:378
本文介绍了在foreach循环中使用cURL(php)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多HTTPS-URLS,必须在其中找到特殊短语,因此我试图在foreach循环中使用cURL,但是它不起作用。

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);
...

如果我仅使用一个HTTPS-URL,则会得到该短语,但是在一个foreach循环中,它不起作用。
有人可以帮助我吗? (:

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..

推荐答案

这可能有帮助:)将结果存储在数组中

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);

这篇关于在foreach循环中使用cURL(php)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆