Using simplexml_load_file to pull from tumblr - timing out every time

后端 未结 3 927
面向向阳花
面向向阳花 2021-01-26 07:49

My site is taking ~45 seconds to load. It\'s because I\'m pulling in some XML from tumblr, but I can\'t figure out if this is my server\'s fault, tumblr\'s fault, or some other

相关标签:
3条回答
  • 2021-01-26 08:09
    curl_setopt(**$s**,CURLOPT_TIMEOUT,5);
    

    Here should be $ch in reply by Ibu I think. But good example anyway! Thank you!

    0 讨论(0)
  • 2021-01-26 08:34

    ok in this case i can suggest you use cUrl

    This way youu can set a timeout and if the page doesnt respond in five seconds you can move on. Documentation

                $url = "http://blog.yaytalent.com/api/read?type=post&start=1&num=2";
                $ch = curl_init(); 
                curl_setopt($ch, CURLOPT_URL, $url); 
                curl_setopt($ch, CURLOPT_HEADER, FALSE); 
                curl_setopt($ch, CURLOPT_NOBODY, FALSE); 
                curl_setopt($s,CURLOPT_TIMEOUT,5); // TIME OUT is 5 seconds
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
                $response = curl_exec($ch); 
                $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
                curl_close($ch); 
    

    I hope that works for you and goodluck in your learning

    UPDATE:

    now you can use your simple xml like this:

    $xml = simplexml_load_string($response);

    0 讨论(0)
  • 2021-01-26 08:34

    You can also do in this way:

    function simplexml_load_file_from_url($url, $timeout = 20){
    
      $context = array('http' => array('timeout' => (int)$timeout));
    
      $data = file_get_contents($url, false, stream_context_create($context));
    
      if(!$data){
        trigger_error('Cannot load data from url: ' . $url, E_USER_NOTICE);
        return false;
      }
    
      return simplexml_load_string($data);
    }  
    
    0 讨论(0)
提交回复
热议问题