Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    require_once(url) not requiring class/file

    Hi

    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
    http://dundeelandlordaccreditation.co.uk/stock/test.php

    Any help would be appreciated.


    Thanks

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #3
    New Coder
    Join Date
    Jan 2012
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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?.

    Thanks

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,469
    Thanks
    13
    Thanked 361 Times in 357 Posts
    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.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    ryanfern86goa (07-19-2012)

  • #5
    New Coder
    Join Date
    Jan 2012
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi Dormilich,

    Thanks for the reply. This my code

    <code>

    GetProducts.php

    <?php

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

    public function __construct($key,$id){
    $this->apiKey=$key;
    $this->UserId=$id;
    }

    public function FetchProducts(){
    $wdsl='http://webservices.smiffys.com/services/products.asmx?WSDL';
    $soapClient= new SoapClient("$wdsl");
    $productCode = '';
    $batchNumber =1;
    $stockParameters=array("apiKey"=>$this->apiKey,"clientID"=>$this->UserId,"ategory"=>"","filterCode"=>"","filterDescription"=>"");
    $this->GetXml($soapClient,"GetStockQuantities_LightVersion_XML",$stockParameters,"Product");

    }


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



    $simpleresult = $result->$methodResultName;

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

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

    }
    ?>

    Product.php

    <?php
    require_once("http://http://dundeelandlordaccreditation.co.uk/stock/GetProducts.php");

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


    ?>
    <html>
    <head><title>Stock</title></head>
    <body>

    <table>
    <tr>
    <th>Product number</th>
    <th>Quantity</th>
    </tr>
    <?php
    foreach($xml->result as $product) {

    ?>

    <tr>
    <td><?php echo $product->Product_Code; ?></td>
    <td><?php echo $product->Available_Stock; ?></td>
    </tr>
    <?php
    }
    ?>
    </table>
    </body>
    </html>
    </code>

    Any help would be greatly appreciated.

    Thanks

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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 [php][/php] or [code][/code] tags to preserve the formatting.

  • Users who have thanked Fou-Lu for this post:

    ryanfern86goa (07-19-2012)

  • #7
    New Coder
    Join Date
    Jan 2012
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi Fou-Lu,

    Thanks again.

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

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

    When I use relative path eg
    require_once("GetProducts.php");

    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
    require_once("http://dundeelandlordaccreditation.co.uk/stock/GetProducts.php");

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

    Thanks

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,469
    Thanks
    13
    Thanked 361 Times in 357 Posts
    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)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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).

  • #10
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by ryanfern86goa View Post
    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.
    I can't really think of anything to write here now...

  • Users who have thanked tangoforce for this post:

    ryanfern86goa (07-19-2012)

  • #11
    New Coder
    Join Date
    Jan 2012
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you all for your great support. Got it straight now. Cheers !


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •