gpt4 book ai didi

php - 在 foreach 循环中使用 cURL (php)

转载 作者:可可西里 更新时间:2023-11-01 01:13:09 25 4
gpt4 key购买 nike

我有很多 HTTPS-URLS,我必须在其中找到一个特殊短语,所以我尝试在 foreach 循环中使用 cURL,但它不起作用。

...
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 循环中它不起作用。有人能帮我吗? (:

请原谅我糟糕的英语..

最佳答案

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

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

关于php - 在 foreach 循环中使用 cURL (php),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47623277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com