connecting to remote servers via php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
krraleigh
Forum Commoner
Posts: 86
Joined: Tue Jul 17, 2007 2:52 pm

connecting to remote servers via php

Post by krraleigh »

I am working from a book that is supposed to be showing me how to connect to a remote server
to retrieve data using php. The idea is that javascript can't and isn't safe to use so the alternative
is to use a proxy server script to do the work.

This first line of code is given to make the connection

Code: Select all

 
    //holds the remote server address and parameters
    $serverAddress = "http://www.random.org/cgi-bin/randum";
    $serverParams = "num=" . $num . //how many random numbers to genrate
                 "&min=" . $min . //the min number to generate
                 "&max=" . $max; // max number to generate
//connecting to a foreign server and get contents
    $randomNumber = file_get_contents($serverAddress . '?' . $serverParams);  
 
if this fails and it does use this code:

Code: Select all

 
    //use if file_get_contents() fails
    $ch = curl_init();
    $timeout = 5; // set to zero for no timeout
    curl_setopt ($ch, CURLOPT_URL, "$serverAddress . '?' . $serverParams");
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $randomNumber = curl_exec($ch);
    curl_close($ch);
    // display file
    echo $randomNumber;
Problem is both of them fail
Can anyone suggest how I might resolve this issue?

Kevin


Post Reply