Accessing another URL for information

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?

Thoughts?
brucegustPHP DeveloperAsked:
Who is Participating?
 
Julian HansenCommented:
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.
<request>
  <type>timesfive</type>
  <value>10</value>
</request>

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>
	<result>$result</result>
</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);

  curl_close($curl);
  $xml = new SimpleXMLElement(html_entity_decode($http));
  
  return $xml;
}

$xml = <<<XML
<request>
  <type>timesfive</type>
  <value>10</value>
</request>
XML;

$xml = getXml($xml, 'http://server/getxml.php');
echo $xml->result;

Open in new window

0
 
GaryCommented:
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.
0
 
DrDamnitCommented:
On your server that has the data, write a script that exports the information as XML and displays it as text. http://www.serverwithdata.com/hiddenscript.php

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

Parse the XML,

Display the data.

Done.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
brucegustPHP DeveloperAuthor Commented:
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.
0
 
Ray PaseurCommented:
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
0
 
DrDamnitCommented:
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.
0
 
brucegustPHP DeveloperAuthor Commented:
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!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.