views:

26

answers:

4

Hi everybody!

I have 2 files on 2 different servers:

file1.php - resides on site 1 - I pass a parameter, and the script echo-ed answer which depends on (is function of) passed parameter - everithink is OK when I access file by browser like

   http://site1.com/file1.php?parameterValue

file2.php - resides on site 2 - file2 has to send a parameter to file1.php AND get echo-ed output from it as variable.

I tried to do it by 3 diferent ways but no ones worked.

way 1. -------

function get_data($url)
{
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}  

$f="http://site1.com/file1.php?parameterValue";
$returned_content = get_data($f);
echo "=== $returned_content ===";exit;

way 2. -------

$f="http://site1.com/file1.php?parameterValue";
$returned_content='';
$file = fopen ($f, "r");
if (!$file) {
    echo "<p>Unable to open remote file.\n";
    exit;
    }
while (!feof ($file))  $returned_content.= fgets ($file, 1024);
fclose($file);
echo "=-= $returned_content =-=";exit;

way 3. -------

$f="http://site1.com/file1.php?parameterValue";
$returned_content=implode('',file($f));
echo "=-= $returned_content =-=";exit;

BUT $returned_content is empty string ...

Can anybody help me? Thanks in advance!

Hristo

+1  A: 

What happens if you try:

<?PHP
$f="http://site1.com/file1.php?parameterValue";
$data = file_get_contents($f);
echo $data;

?

timdev
yes, it works :-)
hristo
A: 

I tested all three of those methods and all worked using

$f = "http://google.com/";

I'd check the configuration of site1 and file1.php. Perhaps it's blocking requests based on User-Agent?

Forrest
thank you my friend!I had a script error in file1All works fine now!
hristo
+1  A: 

You could modify your first version with CURL to check if any errors occured. As is, you're blindly assuming that the curl request worked and just return whatever curl_exec() returned.

At mininum, you should have something like:

$data = curl_exec($ch)
$err = curl_error($ch);
curl_close($ch);
if ($data === FALSE) { // curl_exec returns boolean FALSE if something blew up
   return($err);
} else {
   return($data);
}
Marc B
Thanks MARK B !It really will be useful addition to the script of way 1
hristo
A: 

Hi everybody again!

It was my fault -- my script in file1 was a complicated and I missed one other parameter on which it depends. So, after I correct the script all works fine.

Way 2 and 3 works correct; Way 1 I didnt tested. It is goog idea to use suggestion of MARC B

way supposed by TIMDEV works fine too.

I would like to thank all of you giving help to me so kindly!

Thank you friends!

Best regards Hristo

hristo