为什么只有数组上的最后一个url用于curl

时间:2018-02-22 01:29:31

标签: php arrays curl

我发现我的代码存在错误,当我在数组$ url上放置一些值时,只是在进程上使用了最后一个值。 我真的不知道问题是什么,如果有人帮助我,我会很好

<body>
<form action="" method="post">
    <textarea name="url">site</textarea>
    <input type=submit value="EXPLOITAR">
</form>
</body>
<?php
$link = $_POST['url'];
$a = explode(";", $link);
function geralink($a){
    return("$a".'index.php');
}
$url =array_map("geralink" , $a);
// loop for curl_setopt
foreach( $url as $key => $url){
    $mh = curl_multi_init();
    $ch[$key] = curl_init($url);

    // set URL and other appropriate options
    curl_setopt($ch[$key], CURLOPT_URL, $url );
    curl_setopt($ch[$key], CURLOPT_HEADER, 0);
    curl_setopt($ch[$key], CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');
    curl_setopt($ch[$key], CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch[$key], CURLOPT_RETURNTRANSFER, TRUE);
    curl_multi_add_handle($mh , $ch[$key]);
}
do {
    curl_multi_exec($mh, $running);
    curl_multi_select($mh);
} while ($running > 0);

// loop for the responses codes 
foreach(array_keys($ch) as $key){
    $httpcode = curl_getinfo($ch[$key], CURLINFO_HTTP_CODE);
    $effurl  = curl_getinfo($ch[$key], CURLINFO_EFFECTIVE_URL);
    if(curl_errno($ch[$key]) === TRUE){   
        echo 'Curl error: ' . curl_error($ch[$key]);
    }else{
        echo "CODE: $httpcode URL: $effurl <br>";
        echo "\n";
    }
    curl_multi_remove_handle($mh, $ch[$key]);
}
curl_multi_close( $mh)
?>

抱歉可怕的英国人xd

2 个答案:

答案 0 :(得分:0)

您对urls数组和网址项使用相同的var名称:

$url =array_map("geralink" , $a);
// loop for curl_setopt
foreach( $url as $key => $url){

好多了:

$urls =array_map("geralink" , $a);
// loop for curl_setopt
foreach( $urls as $key => $url){

答案 1 :(得分:0)

您正在循环中初始化curl multi,这就是为什么它仅适用于最后一个url的原因。您必须在循环之前初始化curl multi:

$url =array_map("geralink" , $a);
$mh = curl_multi_init(); //Yes, here

// loop for curl_setopt
foreach( $url as $key => $url){
    //$mh = curl_multi_init(); //Not here