Link to home
Start Free TrialLog in
Avatar of sasnaktiv
sasnaktivFlag for United States of America

asked on

How can I make "include ('specialDOC.php');" function from another server or domain name that I am using?

This is either easy or impossible!
But ...
How can I make "include ('specialDOC.php');" function when 'specialDOC.php' resides on a different server/domain than the php document that's calling on it?
This treatment always fails: include ('http://MyOtherDomainName.com/specialDOC.php');
Require fails too.

I just want to be able to include one 'specialDOC.php' in many other documents that I'm creating on several different sites that I service.

Any ideas?
Avatar of adeelshahid
adeelshahid

use this,

echo file_get_contents('http://MyOtherDomainName.com/specialDOC.php');
Avatar of sasnaktiv

ASKER

Thanks for trying adeelshahid, but these are the errors that are returned:

file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Name or service not known in

[function.file-get-contents]: failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in
so that is disabled, no probs,

try this,

get_web_page('http://MyOtherDomainName.com/specialDOC.php');

function get_web_page( $url )
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "spider", // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}

I'm not getting any errors this time, adeelshahid.
But I'm also not getting 'http://MyOtherDomainName.com/specialDOC.php' into the php document.
What do you think?
What are you trying to retrieve - the raw PHP statements or the browser output stream from the PHP processing at the remove site?
Also, please post the actual URL of your test script so we can test with the live data sample.  If you do not have a test data set yet, please create one and post the URL here. Thanks, ~Ray
Hi Ray,
What I'm trying to do is use one php source to provide variables to several different sites.
When you see the code you will know what I'm trying to achieve. It's just that I can't get it work with any 'absolute url'. It only functions when using a 'relational url'.

specialDOC.php
<? $TextColor1='EE0000'; ?>

Open in new window


specialDOCtest.php
<?php
include ('http://dualcorporation.com/specialDOC.php');
echo ("
<html>
<head>
<TITLE>Experts Sample</TITLE>
</head>
<BODY bgcolor='#FFFFFF' text='#$TextColor1' >
<P>&nbsp;<P>&nbsp;<P>
<span style='FONT-SIZE:18px; line-height:20px; font-weight:normal; color:#$TextColor1; font-family:Arial, sans-serif;'>
<B>All This Text Should Be Red. Is it?</B>
<P>
This treatment fails: \"include ('http://dualcorporation.com/specialDOC.php');\"<BR>
This treatment functions: \"include ('specialDOC.php');\"
<P>
The <I>specialDOC.php</I> contains the following code:
<BR>
&#60;? &#36;TextColor1='EE0000'; ?&#62;
</BODY>
</HTML>
");?>

Open in new window


You can see it fail at both these sites:
'http://dualcorporation.com/specialDOCtest.php'
AND
'http://nybusstories.com/specialDOCtest.php'

Thanks for the help
just change the function into,



function get_web_page( $url )
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "spider", // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );

    echo $content;

    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}
Regarding this: "use one php source to provide variables to several different sites"

That sounds like what you want is called a "web service" and I can show you an example of how it is done.  You read from the remote service with file_get_contents() or CURL.  The service script returns a data string, a set of comma-separated values, or perhaps JSON or an XML string.  It all depends on what you need to get back from the service.

The code for the teaching example of a web service is shown in the code snippet.  You can test it here on my web site by experimenting with this URL:
http://www.laprbass.com/RAY_REST_get_last_name.php?key=ABC&name=Ray&resp=XML

Please read it over - code and comments - and post back with any specific questions.  When you test it, you might want to use View Source to look at the output.

So to sum up, instead of using include() you simply call a script on a remote server by sending it your arguments in the URL GET string.  The remote server responds with an answer that is appropriate to what is in the URL.  This is called REST, and it is how the entire WWW works.  This link will tell you more than you need to know:
http://en.wikipedia.org/wiki/Representational_State_Transfer

Best regards, ~Ray
<?php // RAY_REST_get_last_name.php
error_reporting(E_ALL);



// DEMONSTRATE HOW A RESTFUL WEB SERVICE WORKS
// INPUT FIRST NAME, OUTPUT LAST NAME
// CALLING EXAMPLE:
// file_get_contents('http://laprbass.com/RAY_REST_get_last_name.php?key=ABC&resp=XML&name=Ray');



// OUR DATA MODEL CONTAINS ALL THE ANSWERS - THIS COULD BE A DATA BASE - AS SIMPLE OR COMPLEX AS NEEDED
$dataModel
= array
( 'Brian'   => 'Portlock'
, 'Ray'     => 'Paseur'
, 'Richard' => 'Quadling'
, 'Dave'    => 'Baldwin'
)
;


// SHOULD RESPONSE BE PLAIN TEXT OR XML FORMAT
$alpha = '';
$omega = '';
if ( (isset($_GET["resp"])) && ($_GET["resp"] == 'XML') )
{
    $alpha = '<response>';
    $omega = '</response>';
}



// TEST THE API KEY
$key = FALSE;
if (isset($_GET["key"])) $key = $_GET["key"];
if ($key !== 'ABC') die($alpha . 'BOGUS API KEY' . $omega);



// LOOK UP THE LAST NAME
$name="?";
if (isset($_GET["name"])) $name = $_GET["name"];

// IF THE URL NAME IS FOUND IN THE DATA MODEL
if (array_key_exists($name, $dataModel))
{
    // RETURNS THE LAST NAME FROM THE DATA MODEL
    die($alpha . "$dataModel[$name]" . $omega);
}

// RETURNS THE UNKNOWN NAME INDICATOR
else die($alpha . 'UNKNOWN' . $omega);

Open in new window

Thanks for the help folks.
It looks like there are two approaches. And I have results for each:

Adeel, I used your code verbatim and like the first attempt I'm not getting any errors, but also the 'specialDOC.php' is not being 'included' into the php document that calls on it.

Ray, I'm following the links you provided. It will take quite a while for me to digest it all. So of course I took a short cut and used the code you provided -- The result was 'BOGUS API KEY' -- I guess that means it failed, or am I doing something ass-backwards again?

I want you guys to know that I really do appreciate the help.
Did you run the script exactly the way I posted it?
http://www.laprbass.com/RAY_REST_get_last_name.php?key=ABC&name=Ray&resp=XML

If so, you should not have gotten a message that said BOGUS API KEY.  The script I posted is a teaching example, not intended for use in your applications, but just a code set that illustrates the principles.  See how to call it (or another web service, like your own) in the comment on line 9.
I'll tell you exactly what I did Ray.
I pasted your entire code into a new document and named it 'specialDOC_P.php'.
Then I uploaded it to the server where it can be reached at 'http://nybusstories.com/specialDOC_P.php'
Then I called on that URL and got 'BOGUS API KEY'.

When I call on the URL you sent, I just get your last name.
'http://www.laprbass.com/RAY_REST_get_last_name.php?key=ABC&name=Ray&resp=XML'

Line 9 of your code has some similarities to Adeel's first post.

I get the feeling that I'm close but I must be doing something stupid.
Sigh.  Click here.  View Source.
http://nybusstories.com/specialDOC_P.php?key=ABC&name=Ray&resp=XML

The script is a computer program.  It takes its input from the URL arguments and uses the input to determine what output to produce.  At this point I am going to step back from the question and recommend that you hire a professional developer to write this code for you.  There is nothing wrong with not being able to understand the intricacies of PHP, but it would be unfortunate for you to waste too much of your time on something that a professional could do in a matter of minutes.

If you're interested in learning more about how PHP can be used to develop web sites, this is a really great book with good examples and an easy-to-understand writing style.  Now in its fourth printing, it has been in my professional library since version 1.
http://www.sitepoint.com/books/phpmysql4/

All the best, ~Ray
Avatar of Bernard Savonet
just a comment: allowing "remote include" would create a serious safety hole, so that's why in the last versions includes are limited to the include path and to the directories below "the current one"

Just imagine that your site is tightly protected but that the remote is less... someone might change your file to be included...

I got a "neglected question alert" on this.  I can see from this URL that you still have the demonstration script in place.
http://nybusstories.com/specialDOC_P.php?key=ABC&name=Ray&resp=XML

Now that you have had a week to digest the responses, do you understand what I posted at ID:35064001 and ID:35086015?  If not, please tell us what is missing from your understanding.  Thanks and regards, ~Ray
Hi Ray,
I can't tell you what it is that I don't understand because I don't know what it is that I don't understand.
All I can do is show you what I have tried (using your code).
My goal is to get "CallON_specialDOC_1P.php" to 'include' "specialDOC_1P.php".
Hopefully, this will give you an idea of what it is that I don't understand.
Thanks

URL-1: "http://nybusstories.com/CallON_specialDOC_1P.php"
CallON_specialDOC_1P.php
<?php
error_reporting(E_ALL);

file_get_contents('http://dualcorporation.com/specialDOC_1P.php?key=ABC&resp=XML&name=Ray');

echo ("
<html>
<head>
<TITLE>Experts Sample</TITLE>
</head>
<BODY bgcolor='#FFFFFF' text='#$TextColor1' >
<P>&nbsp;<P>&nbsp;<P>
<span style='FONT-SIZE:18px; line-height:20px; font-weight:normal; color:#$TextColor1; font-family:Arial, sans-serif;'>
<B>All This Text Should Be Red. Is it?</B>
<P>
<P>
<I>specialDOC_1.php</I> contains the following code:
<BR>
&#60;? &#36;TextColor1='EE0000'; ?&#62;
</BODY>
</HTML>
");?>

Open in new window


URL-2 "http://dualcorporation.com/specialDOC_1P.php"
"specialDOC_1P.php"
<?php // RAY_REST_get_last_name.php
error_reporting(E_ALL);



// DEMONSTRATE HOW A RESTFUL WEB SERVICE WORKS
// INPUT FIRST NAME, OUTPUT LAST NAME
// CALLING EXAMPLE:
// file_get_contents('http://laprbass.com/RAY_REST_get_last_name.php?key=ABC&resp=XML&name=Ray');



// OUR DATA MODEL CONTAINS ALL THE ANSWERS - THIS COULD BE A DATA BASE - AS SIMPLE OR COMPLEX AS NEEDED
$dataModel
= array
( 'Brian'   => 'Portlock'
, 'Ray'     => '$TextColor1='EE0000';'    //NOTICE THIS CHANGE. WHEN I CHANGE THE CALL TO 'Brian' I DO NOT GET 'Portlock' RETURNED IN 'CallON_specialDOC_1P.php'
)
;


// SHOULD RESPONSE BE PLAIN TEXT OR XML FORMAT
$alpha = '';
$omega = '';
if ( (isset($_GET["resp"])) && ($_GET["resp"] == 'XML') )
{
    $alpha = '<response>';
    $omega = '</response>';
}



// TEST THE API KEY
$key = FALSE;
if (isset($_GET["key"])) $key = $_GET["key"];
if ($key !== 'ABC') die($alpha . 'BOGUS API KEY' . $omega);



// LOOK UP THE LAST NAME
$name="?";
if (isset($_GET["name"])) $name = $_GET["name"];

// IF THE URL NAME IS FOUND IN THE DATA MODEL
if (array_key_exists($name, $dataModel))
{
    // RETURNS THE LAST NAME FROM THE DATA MODEL
    die($alpha . "$dataModel[$name]" . $omega);
}

// RETURNS THE UNKNOWN NAME INDICATOR
else die($alpha . 'UNKNOWN' . $omega);

Open in new window

One of the advantages of GET-method web services is that you can test them by just typing the URL into a browser window.  Example -- click this link.
http://dualcorporation.com/specialDOC_1P.php

Once you get the web service script running, you can think about the script that is calling the web service.  

In that script, consider this statement:
file_get_contents('http://dualcorporation.com/specialDOC_1P.php?key=ABC&resp=XML&name=Ray');

Please look up the function, read the online man page, and see that it returns a string or FALSE on failure.  
http://php.net/manual/en/function.file-get-contents.php

The code as written calls the function, but does not assign the return value to a variable, so the statement is useless.  It is things like this that lead me to believe that you should step back from the problem and take some time to learn PHP.  This book can help, so I will recommend it again.
http://www.sitepoint.com/books/phpmysql4/

The instant question has really been going on for a long time now and seems to be going nowhere.  If you're in need of this for something that has economic value, I strongly recommend that you hire a professional developer to write the code for you.  It will get done much faster that way.  If it is just a learning exercise for you, then start with the SitePoint book and build yourself a foundation of knowledge.

Best regards, ~Ray
Hi Ray,
I'm still missing something. So I'd like to take your advice about hiring a professional PHP person (PPHPP). Where can I find somebody who is reliable, can understand my communications, and will honor an NDA?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I want to put this on hold and return to it later. But I don't see any such option.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, your information was helpful. I did not know about a path to a "Hire Me" button.
It's much appreciated.
Hi, you forgot to formally close the question
Thanks,
But I want to come back to this when I have time in order to learn more.
Sas
Please consider closing the question for now, as a courtesy to those who answered.
You can ask this question again later... presumably you will have more experience and will probably ask something different.
Okay, how should I close it?
Page https://www.experts-exchange.com/help/viewHelpPage.jsp?helpPageID=24
will tell all.

Look back at the answers and how you feel the contributed to the solution, and allocate / split points accordingly.
On top of points, you will grade the solution A B or C.

And if you have still a problem, click the "request attention" link that displays just under your question, at the top of this page: page admin will guide you thru
This was closed and points were awarded
Hi Sasnaktiv,

Seeems you forgot to allocate points and to close as you intended B-)
Thank you
B-) glad  we could help, thx for the grade and points