View Full Version : require_once(url) not requiring class/file

07-18-2012, 09:19 PM

I have a file on remote server that has a class suppose A that I need to use.
Now when I do this require_once("http://abc.com/A.php"); form file suppose test.php .(Note file test.php is also in the folder stock and I know I could use relative path but this is only for testing purpose, production would require A.php to reside on a different server)

and when I now instantiate that class like

$result=new A();

I get this error
Fatal error: Class 'A' not found in /home2/dundeela/public_html/stock/A.php on line 4

Now if I give require_once("A.php") instead of require_once("http://abc.com/A.php") it seems to work.

Heres the non working example

Any help would be appreciated.


07-18-2012, 09:27 PM
Require_once in your code here is using the http wrapper. That automatically is parsed remotely and only the results are returned. If it truly is remote, the only way to get a definition is if the parsed results are that of a class definition.
Summary would be: you cannot include information including classes, functions, variables or constants from a remote location, at least not with an included http file. You can use other protocols such as ftp if you have privilege, or write soap handlers if you control both sites.

07-19-2012, 06:03 AM
Hi Fou-Lu,

Thanks for your enlightening reply, really appreciated. Just out of curiosity, is there a way to include or require classes and function from a file stored on a remote server?.


07-19-2012, 10:08 AM
not via the HTTP protocol. otherwise writing PHP code would be pointless as there would not be any security possible.

an option could be to use a remote API (SOAP, etc.). i.e. you don’t include the code but let the remote server do the necessary calculation/processing.

07-19-2012, 05:20 PM
Hi Dormilich,

Thanks for the reply. This my code




class GetProducts{
private $apiKey;
private $UserId;
public $result;

public function __construct($key,$id){

public function FetchProducts(){
$soapClient= new SoapClient("$wdsl");
$productCode = '';
$batchNumber =1;


private function GetXml($soapClient,$method,$parameters,$resultSetTag){
try {
$methodResultName = $method."Result"; //GetStockQuantities_LightVersion_XMLResult
$result = $soapClient->$method($parameters);

$simpleresult = $result->$methodResultName;

$dom=new DOMDocument();
//echo" product 1=".$xml->Product[1]->Product_Code[0];
$this->result= $xml->xpath("//$resultSetTag");
//echo"product count= ".$products." count= ". count($products);
return $this->result;

}catch (SoapFault $exception) {
echo $exception;




$xml=new GetProducts("xxxxxxxx","xxxxxx");


<th>Product number</th>
foreach($xml->result as $product) {


<td><?php echo $product->Product_Code; ?></td>
<td><?php echo $product->Available_Stock; ?></td>

Any help would be greatly appreciated.


07-19-2012, 06:12 PM
This does you no good. There is no output from the GetProducts.php script, so including it into the Product.php script will have no effect (plus there is no way you have an http://http:// file). Since its HTTP wrapped, you do not have a class called GetProducts to work with, nor any functions declared within it.
Also in the future please wrap your code in
tags to preserve the formatting.

07-19-2012, 06:43 PM
Hi Fou-Lu,

Thanks again.

Sorry it was my mistake, its require_once("http://dundeelandlordaccreditation.co.uk/stock/GetProducts.php");
and not

The class getProduct and its functions are defined in it if you see from above.

When I use relative path eg

assuming both GetProducts.php and Products.php are in the same directory it works i.e the class GetProducts.php is available to use only when I specify the abosolute url like

The class is not loaded and hence gives me fatal error saying class GetProducts not found.


07-19-2012, 06:58 PM
well, as has been explained previously, require_once("http://dundeelandlordaccreditation.co.uk/stock/GetProducts.php"); does not include PHP code, it includes the output code, which is created when executing the GetProducts.php script. (and since there is no output from that script, it includes an empty string)

07-19-2012, 07:10 PM
That's right.
If you serve a file via HTTP, it is automatically handled by the webserver on that machine. So if you ask for a .php file, it is processed as PHP code, and only the results are returned as typically text/html. You cannot use variables, constants, functions, classes, or anything at all that was created within origin code itself as this is parsed by the webserver before serving it to a client.

The easiest way to look at it is this: with using an http include as you have above, it's results are exactly the same as they would be if you point your browser at that script (which in this case is no results).

07-19-2012, 07:36 PM
Just out of curiosity, is there a way to include or require classes and function from a file stored on a remote server?.

Yes / No.

No: Not as a .php file. As others have explained your main scripts http call using require_once('http://') will mean that the remote machines RUNS the php file and will print any output it generates. It will not print out the php code - just like this forum. You look at showthread.php and it prints out html not php code.

Yes: If you store the code in a .txt file on the remote server then you can download it using file_get_contents() or cURL and then run it through the eval() function to make it run as hardcoded php inside the main script. The downside to this is that anyone who stumbles upon it can see your php (and potentially any weaknesses) / use it themselves / hack the server and modify the code that is then run remotely on your main site on their behalf etc.

07-19-2012, 08:07 PM
Thank you all for your great support. Got it straight now. Cheers !