Accessing another URL for information

Posted on 2012-09-07
Last Modified: 2012-09-24
I've got a situation where I'm having to retrieve data from another server.

The page my user will access has on it a statement that says, "Look how we've helped our members thus far:" After the ":," there will be a number that represents the number of pounds their membership has lost that year.

That number will be computed using a database and script sitting on another server. Obviously it would be a slam dunk if I could put everything on the server my user is accessing, but that isn't the case, at least right now.

So, is there a way I could use a "GET" dynamic to retrieve the number I need?

Question by:brucegust
    LVL 58

    Expert Comment

    If that is all that is on the page then just do an ajax call and strip (split) everything before ":,"
    Plain ole js or jquery if you can.
    LVL 32

    Assisted Solution

    On your server that has the data, write a script that exports the information as XML and displays it as text.

    On the server that is displaying the data, use cURL to grabt that XML file.

    Parse the XML,

    Display the data.


    Author Comment

    Doctor, I'm digging your suggestion and while I'm familiar with cURL as far as some of the ecommerce stuff I've done, I would welcome an example if you could provide one as well as an XML example of how that would work with what I'm trying to do.

    Or, if there's an online tutorial, that would be great too.
    LVL 50

    Accepted Solution

    Here is some code I used in a site a while back.

    Pass in an xml packet with parameters - get an xml string back
    The XML (Sample with function type and value) - service returns value * 5.

    Open in new window

    The Service
    $data = file_get_contents('php://input');
    $result = 0;
    if (!empty($data)) {
    	$xml = new SimpleXMLElement($data);
    	$result = $xml->value * 5;
    echo "<response>

    Open in new window

    The Client
    function getXML($xml, $url, $timeout=30, $encode=true)
      $curl = curl_init($url);
      curl_setopt($curl, CURLOPT_NOSIGNAL, 1);
      curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
      curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
      curl_setopt($curl, CURLOPT_POST, 1);
      curl_setopt($curl, CURLOPT_HEADER, 0);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
      curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT    5.0'); 
      curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
      $http = curl_exec($curl); 
      $info = curl_getinfo($curl);
      $xml = new SimpleXMLElement(html_entity_decode($http));
      return $xml;
    $xml = <<<XML
    $xml = getXml($xml, 'http://server/getxml.php');
    echo $xml->result;

    Open in new window

    LVL 107

    Expert Comment

    by:Ray Paseur
    Please post the URL of the foreign server.  I expect I can give you a tested and working code sample that will return the number.  Thanks, ~Ray
    LVL 32

    Expert Comment

    julianH has a great example already.

    If you tell us what the data is / what it looks like, we'll be able to help you with even more.

    Author Comment

    Hey, guys!

    Thanks for your time, one and all.

    I was able to work around the problem in a way where I just displayed the data on a screen that was coming from the URL where the data was being stored. I'm award points to those who logged in and thank you so much!

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Suggested Solutions

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now