我创建了一个脚本,它从单个网站/ URL的标题中提取HTTP响应/代码。这很好。
<!-- Form for URL input -->
<div style="margin: 20% auto; max-width:250px; font-size:18px; font-family: monospace;">
<form name="open_linktest" action="" method='GET'>
<input type="text" name="linktest" value="" style="font-size: 18px; width: 250px;">
<button type="submit" name="submit" style="font-size: 22px; padding: 4px;">Check IP / URL</button>
</form>
<!-- GET SELECT VALUES USING PHP -->
<?php
if(isset($_GET['linktest'])){
$linktest = $_GET['linktest'];
// Using $linktest variable
}
$url = "$linktest"; // $url GET data from 'linktest' form input
$ch = curl_init($url); // initiate curl on $url
curl_setopt($ch, CURLOPT_TIMEOUT, 20); // Set curl options
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($ch, CURLOPT_HEADER,true);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode>=200 && $httpcode<400){
echo "$linktest" . '<br />Status = Running Okay <br />';
echo "HTTP Code Response: $httpcode";
} else {
echo "$linktest" . '<br />Status = Not working <br />';
echo "HTTP Code Response: $httpcode";
}
?>
</div>
我希望输入多个网址 - 然后让它拍出响应代码&amp;每个人的状态。 (还将在稍后添加IP-ping)。
<?php
$urls = array(
"http://google.com"
"http://php.net"
"https://pfsense.dataquestdigital.com.au"
);
// Class to run parallel GET requests and return the transfer
class ParallelGet
{
function __construct($urls)
{
// Create get requests for each URL
$mh = curl_multi_init();
foreach($urls as $i => $url)
{
$ch[$i] = curl_init($url);
curl_setopt($bhash4win_Curl, CURLOPT_HEADER, 1);
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, 1);
// extra lines from original single curl code to call http header info
curl_setopt($ch[$i], CURLINFO_HEADER_OUT, true);
curl_setopt($ch[$i], CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch[$i], CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch[$i], CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
//
curl_multi_add_handle($mh, $ch[$i]);
}
// Start performing the request - $mh is multi $ch
do {
$execReturnValue = curl_multi_exec($mh, $runningHandles);
} while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);
// Loop and continue processing the request
while ($runningHandles && $execReturnValue == CURLM_OK) {
// Wait forever for network
$numberReady = curl_multi_select($mh);
if ($numberReady != -1) {
// Pull in any new data, or at least handle timeouts
do {
$execReturnValue = curl_multi_exec($mh, $runningHandles);
} while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);
}
}
// Check for any errors
if ($execReturnValue != CURLM_OK) {
trigger_error("Curl multi read error $execReturnValue\n", E_USER_WARNING);
}
// Extract the content - $urls giving $i loop counter variables = put into $url for each loop
foreach($urls as $i => $url)
{
// Check for errors
$curlError = curl_error($ch[$i]);
if($curlError == "") {
$res[$i] = curl_multi_getcontent($ch[$i]);
$res[$i] = curl_multi_getinfo();
echo $res[$i]."<br />\n";
echo "hello";
} else {
print $i."error to get a result"."\n";
echo "borked";
}
// Remove and close the handle
curl_multi_remove_handle($mh, $ch[$i]);
curl_close($ch[$i]);
}
// Clean up the curl_multi handle
curl_multi_close($mh);
}
}
?>
有更简单的方法吗?我是朝着正确的方向前进 - 有一个URL的数组,运行foreach / curl_multi_init ..
是否可以使用curl_multi_getinfo()来提取标题?