在PHP中从外部网页提取数据的问题

I have a script that is responsible for extracting names of people from an external web page by passing an ID as a parameter. Note: The information provided by this external website is public access, everyone can check this data. This is the code that I created:

      function names($ids)
        {
        $url = 'https://www.exampledomain.com/es/query_data_example?name=&id='.$ids;


        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER,array("Accept-Lenguage: es-es,es"));
        curl_setopt($ch, CURLOPT_TIMEOUT,10);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $html = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);

         preg_match_all('/<tr class="odd"><td><a href="(.*?)">/',$html ,$matches);

         if (count($matches[1] == 0)) 
         {
            $result = "";
         }
         else if(count($matches[1] == 1)) 
         {
            $result = $matches[1][0];
            $result = str_replace('/es/person/','', $result);
            $result = substr($result, 0,-12);
            $result = str_replace('-', ' ', $result);
            $result = ucwords($result);
         }

        return $result; 
            }

Note2: in the variable $ url I have placed an example url, it is not the real url. It's just an exact example of the original URL that I use in my code.

I make the call to the function, and I show the result with an echo:

  $info = names('8476756848');
  echo $info;

and everything is perfect, I extracted the name of the person to whom that id belongs.

The problem arises when I try to query that function within a for(or while) loop, since I have an array with many ids

    $myids = ["2809475460", "2332318975", "2587100534", "2574144252", "2611639906", "2815870980", "0924497817", "2883119946", "2376743158", "2387362041", "2804754226", "2332833975", "258971534", "2574165252", "2619016306", "2887098054", "2449781007", "2008819946", "2763767158", "2399362041", "2832047546", "2331228975", "2965871534", "2574501252", "2809475460", "2332318975", "2587100534", "2574144252", "2611639906", "2815870980", "0924497817", "2883119946", "2376743158", "2387362041", "2804754226", "2332833975", "258971534", "2574165252", "2619016306", "2887098054", "2449781007", "2008819946", "2763767158", "2399362041", "2832047546", "2331228975", "2965871534", "2574501252", "2809475460", "2332318975", "2587100534", "2574144252", "2611639906", "2815870980", "0924497817", "2883119946", "2376743158", "2387362041", "2804754226", "2332833975", "258971534", "2574165252", "2619016306", "2887098054", "2449781007", "2008819946", "2763767158", "2399362041", "2832047546", "2331228975", "2965871534", "2574501252"];
    //Note: These data are for example only, they are not the real ids.

      $size = count($myids);

     for ($i=0; $i < $size; $i++) 
        { 
        //sleep(20); 

        $data = names($myids[$i]);

        echo "ID IS:  " . $myids[$i] . "<br> THE NAME IS: " . $data . "<br><br>";
        }

The result is something like this:

        ID IS: 258971534
        THE NAME IS: 

        ID IS: 2883119946
        THE NAME IS:

and so on. I mean, it shows me the Ids but the names do not extract them from the names function. It shows me the whole list of ids but in the case of the names it does not show me any, as if the function names does not work. If I put only 3 ids in the array and run the for loop again, then it gives me the names of those 3 ids, because they are few. But when the array contains many ids, then the function already returns no names. It is as if the multiple requests do not accept them or limit them, I do not know. I have placed the function set_time_limit (0) at the beginning of my php file; to avoid that I get the error of excess time of 30 seconds.

because I thought that was why the function was not working, but it did not work. Also try placing a sleep (20) inside the cycle, before calling the function names to see if it was that it was making many requests very quickly to said web page but it did not work either.

This script is already in production on a server that I have hired and I have this problem that prevents my script from working properly.

Note: There may be arrays with more than 2000 ids or I am even preparing a script that will read files .txt and .csv that will contain more than 10000 ids, which I will extract from each file and call the function name, and then those ids and the names will be saved in a table from a mysql database.

Someone will know why names are not extracted when there are many ids but when they are few for example 1 or 10 the function name does work?