title: PHP curl request on remote images taking forever, what can I do to improve my code?
body: <p>Here is the code I have, I cannot work out what is causing the delays?</p>
  
  <p>To load the remote URL it doesn't take more than a second, should I pass a user_agent?</p>
  
  <p>Please excuse if this is a stupid question, I'm new to PHP, would it be worth setting a timeout as part of the curl request?</p>
  
  <pre><code>&lt;?php
  $url = $_GET['url'];
  
  if(!filter_var($url, FILTER_VALIDATE_URL)) {
  ?&gt;
  {"errors":1,"message":"The URL was not valid"}
  &lt;?php
      die();
  }
  
  
  $p=parse_url($url);
  $baseurl = $p['scheme'] . '://' . $p['host'];
  $path_parts = pathinfo($url);
  $current_dir = $path_parts['dirname'];
  
  
  Function check_img($file) {
  
     $x = @getimagesize($file);
  
     if ($x) {
  
     switch ($x['mime']) {
        case "image/gif" || "image/jpeg" || "image/png":
           $response = true;
           break;
        default:
           $response = false;
           break;
     }
  
  } else {
           $response = false;
  }
  
     return $response;    
  }
  
  
  function ranger($url){
      $headers = array(
      "Range: bytes=0-605768"
      );
  
      $curl = curl_init($url);
      curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
      $data = curl_exec($curl);
      curl_close($curl);
      return $data;
  }
  
  
  function file_get_contents_curl($url)
  {
      $ch = curl_init();
  
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  
  
      $data = curl_exec($ch);
      curl_close($ch);
  
      return $data;
  }
  
  $html = file_get_contents_curl($url);
  
  //parsing begins here:
  $doc = new DOMDocument();
  @$doc-&gt;loadHTML($html);
  $nodes = $doc-&gt;getElementsByTagName('title');
  
  // Get all image tags
      $imageTags = $doc-&gt;getElementsByTagName('img');
      $numImages = $doc-&gt;getElementsByTagName('img')-&gt;length;
  
  //get and display what you need:
  $metas = $doc-&gt;getElementsByTagName('meta');
  
  for ($i = 0; $i &lt; $metas-&gt;length; $i++)
  {
      $meta = $metas-&gt;item($i);
  
      if($meta-&gt;getAttribute('property') == 'og:image' || $meta-&gt;getAttribute('name') == 'og:image')
          $fb_image = $meta-&gt;getAttribute('content');
          $fb_image = isset($fb_image) ? $fb_image : ''; 
  
  }
  
  
  ?&gt;
  {
      "resource_images": {
          "url" : "&lt;?php echo $url?&gt;",
          "baseurl" : "&lt;?php echo $baseurl?&gt;",
          "fb" : "&lt;?php echo $fb_image?&gt;",
          "images" : [&lt;?php
      $i = 0;
  
  $image_results = array();
  $numItems = count($imageTags);
  
  if ($fb_image !== '') {
      $image_results[] = $fb_image;   
  }
  
      foreach($imageTags as $tag) {
  
              if ($i &gt;= 25) {
                  break;
              }
  
          if (substr($tag-&gt;getAttribute('src'),0,4) === 'http') {
              $img = $tag-&gt;getAttribute('src');
          } elseif (substr($tag-&gt;getAttribute('src'),0,1) === '/') {
              $img = $baseurl . $tag-&gt;getAttribute('src');
          } else {
              $img = $current_dir . $tag-&gt;getAttribute('src');            
          }
  
  $exists = check_img($img);
  
  
  if ($exists) {
      $raw = ranger($img);
  
          $im = imagecreatefromstring($raw);
          $width = imagesx($im);
          $height = imagesy($im);
  
          if ($width &gt; 300) {
              $image_results[] = str_replace('"', "", $img);
          }
  if(++$i &lt; $numItems &amp;&amp; ++$i &lt; 25) {
      echo ",";
    }   
  
       }
  }
  
  
     $i = 0;
  foreach($image_results as $img_url) {
  
  ?&gt;
  {
                          "url" : "&lt;?php echo str_replace('"', "", $img_url);?&gt;",
                          "count" : &lt;?php echo count($image_results)?&gt;
                      }
  &lt;?php   
  if(++$i &lt; count($image_results) &amp;&amp; $i &lt; 15) {
      echo ",";
    }   
  
  }?&gt;
  
  
          ]
      }
  }
  </code></pre>