2016-04-08 7 views
2

Ich habe ein PHP-Skript für ein URL-Status-Checker-Tool, das die angegebenen URLs überprüft und die mit 404-Fehler anzeigt.php - curl_multi "Ungültige Zeichen gefunden in URL"

StatusCheckerRequest hat die Eingabe mit "\ n" voneinander getrennt URLs

public function PostStatusChecker(StatusCheckerRequest $request){ 
    $urls = $request->source; 
    $seperateURLs = explode("\n", $urls); 
    // -- create all the individual cURL handles and set their options 
    $curl_handles = array(); 
    foreach ($seperateURLs as $url) { 
     $curl_handles[$url] = curl_init(); 
     curl_setopt($curl_handles[$url], CURLOPT_URL, $url); 
     curl_setopt($curl_handles[$url], CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($curl_handles[$url], CURLOPT_CONNECTTIMEOUT, 20); 
     curl_setopt($curl_handles[$url], CURLOPT_SSL_VERIFYPEER, false); 
    } 
    // -- start going through the cURL handles and running them 
    $curl_multi_handle = curl_multi_init(); 
    $i = 0; // count where we are in the list so we can break up the runs into smaller blocks 
    $block = array(); // to accumulate the curl_handles for each group we'll run simultaneously 
    $results = array(); 
    $curlErrors = array(); 
    foreach ($curl_handles as $a_curl_handle) { 
     $i++; // increment the position-counter 

     // add the handle to the curl_multi_handle and to our tracking "block" 
     curl_multi_add_handle($curl_multi_handle, $a_curl_handle); 
     $block[] = $a_curl_handle; 

     // -- check to see if we've got a "full block" to run or if we're at the end of out list of handles 
     if (($i % BLOCK_SIZE == 0) or ($i == count($curl_handles))) { 
      // -- run the block 
      $running = NULL; 
      do { 
       // track the previous loop's number of handles still running so we can tell if it changes 
       $running_before = $running; 

       // run the block or check on the running block and get the number of sites still running in $running 
       curl_multi_exec($curl_multi_handle, $running); 
       print_r (curl_multi_info_read($curl_multi_handle)); 
      } while ($running > 0); 


      // -- once the number still running is 0, curl_multi_ is done, so check the results 
      foreach ($block as $handle) { 
       // HTTP response code 
       $code = curl_getinfo($handle, CURLINFO_HTTP_CODE); 
       $results['httpCode'][] = $code; 

       // cURL error number 
       $curl_errno = curl_errno($handle); 
       $results['curlErrorNo'][] = $curl_errno; 

       // cURL error message 
       $curl_error = curl_error($handle); 
       $results['curlErrorMessage'][] = $curl_error;   

       // remove the (used) handle from the curl_multi_handle 
       curl_multi_remove_handle($curl_multi_handle, $handle); 
      } 

      // reset the block to empty, since we've run its curl_handles 
      $block = array(); 
     } 
    } 
    // close the curl_multi_handle once we're done 
    curl_multi_close($curl_multi_handle); 
    print_r($results); 
    die(); 
} 

I verwendet, die ein Beispiel für curl_multi_exec aus Stapelüberlauf, und wenn ich überprüfen Sie die Ergebnisse mit diesen URLs:

Array 
(
    [0] => stackoverfloww.com 
    [1] => www.laravel2.com 
    [2] => http://stackoverflow.com 
    [3] => http://laravel.com 
) 

The Ausgabe ist

[httpCode] => Array 
(
    [0] => 0 
    [1] => 0 
    [2] => 0 
    [3] => 301 
) 

[curlErrorMessage] => Array 
(
    [0] => Illegal characters found in URL 
    [1] => Illegal characters found in URL 
    [2] => Illegal characters found in URL 
    [3] => 
) 

Ich versuchte mit verschiedenen Eingaben und die Ergebnisse sind immer die letzte URL gibt 200 oder 301 zurück, und die anderen sind alle 0. Ich überprüfe auch die Ergebnisse von curl_multi_info_read und die Ergebnisse sind alle 3 für die URLs des "ungültigen Zeichens gefunden", und der Wert des letzten ist 0.

Kannst du bitte helfen, was falsch ist? Vielen Dank.

Antwort

1

Eine schnelle Suche des Quellcodes cURL zeigt, daß dieser Fehler kommt von der Tatsache, dass die URL zu CURLOPT_URL\r enthält und/oder \n die Zeichen zugeführt wird.

Von lib/url.c:

/* We might pass the entire URL into the request so we need to make sure 
    * there are no bad characters in there.*/ 
    if(strpbrk(data->change.url, "\r\n")) { 
    failf(data, "Illegal characters found in URL"); 
    return CURLE_URL_MALFORMAT; 
    } 

Sie sollten die URL durch $url = trim($url); laufen, da es wahrscheinlich ist verbleibenden \r oder \n am Ende der URL.