[PHP] Best way to get content from a remote server?

Results 1 to 4 of 4
  1. #1
    Grand Master RastaLulz is offline
    Grand MasterRank
    Dec 2007 Join Date
    EarthLocation
    3,326Posts

    [PHP] Best way to get content from a remote server?

    Hey guys,

    I was wondering what the best way to get content from a remote server was. I want to be able to get content from my site, which returns a result of 1-6 based on the data requested. I need it to be able to time out within two seconds.

    Here's an example:
    PHP Code:
    <?php
    // Create the stream context
    $context stream_context_create(array(
        
    'http' => array(
            
    'timeout' => 2      // Timeout in seconds
        
    )
    ));

    // Fetch the URL's contents
    $contents file_get_contents('http://example.com/get_data.php?ip='.$_SERVER['REMOTE_SERVER'], 0$context);

    // Check for empties
    if (!empty($contents))
    {
        if(
    $contents == '1') {
          echo 
    'You have successfully voted.';
        }else{
          echo 
    'You have not voted yet, please click here.';
        }
    }
    else
    {
        
    // A terrible, terrible disaster occurs
    }
    Although, I'm sure cURL is more efficient, but I'm not entirely sure.


  2. #2
    Infraction Baɴɴed holthelper is offline
    Grand MasterRank
    Apr 2008 Join Date
    1,765Posts

    Re: [PHP] Best way to get content from a remote server?

    use cURL and pass it through DOMDocument

  3. #3
    Laravel Core Programmer Jangan is offline
    DeveloperRank
    Jul 2007 Join Date
    Dubai, UAELocation
    2,112Posts

    Re: [PHP] Best way to get content from a remote server?

    Um, I never really played with steam context but yours looks a bit incomplete!

    I am pretty sure you NEED a method/content/header to complete the function!

    PHP Code:
    $context stream_context_create(array( 
          
    'http' => array( 
          
    'method'  => 'POST'
          
    'header'  => Whatever...., 
          
    'content' => whatever..., 
          
    'timeout' => 2
        ), 
      )); 
    and how are you exactly calling it?

  4. #4
    Sorcerer Supreme Tony is offline
    Member +Rank
    Feb 2011 Join Date
    349Posts

    Re: [PHP] Best way to get content from a remote server?

    A big note that I hope will help some one. Something that is not mentioned in the documentation, is that when php is compiled --with-curlwrappers,

    So, instead of:

    PHP Code:
    <?php 
    $opts 
    = array( 
      
    'http'=>array( 
        
    'method'=>"GET"
        
    'header'=>"Accept-language: en\r\n" 
                  
    "Cookie: foo=bar\r\n" 
      

    ); 

    $context stream_context_create($opts); 
    ?>
    You would setup the header this way:

    PHP Code:
    <?php 
    $opts 
    = array( 
      
    'http'=>array( 
        
    'method'=>"GET"
        
    'header'=>array("Accept-language: en"
                               
    "Cookie: foo=bar"
                               
    "Custom-Header: value"
      ) 
    ); 

    $context stream_context_create($opts); 
    ?>
    This will work.



Advertisement