1

I am looking to code scripts, but I would like to know how would I let's say get data from one website (site1.com - let's say the page contains ONLY text, IE: abc123) and I want another website to be able to grab this data (site2.com) in a PHP format, no iframes or anything, if possible.

How would I go about doing such? I want to code API like scripts, so people can add them to their site and what not, for example; string generator (site1.com will generate the string, and site2.com will be able to use the string in a php format)

And the way I would like to learn is the same as above, AND also a way where site2.com can choose the length of the returned value. Like site1.com will be setup obviously to return let's say if site2 wanted a string of 5 digits, site1 will generate the string and simply return it in the page (site1.com/gen?leng=5 could work, but its the matter of getting it displayed on the site..)

I want site1 to generate a string, per say. And I want an external site (site2) to be able to obtain this string and use it in PHP format, like $string = site1generatedstring

Reverb
  • 953
  • 2
  • 12
  • 17
  • I don't understand how these are connected. Could you please edit your question and make it more clear what you're trying to accomplish? – Amal Murali Dec 15 '13 at 17:47
  • I just said what I am trying to accomplish. I want site1 to generate a string, per say. And I want an external site (site2) to be able to obtain this string and use it in PHP format, like $string = site1generatedstring – Reverb Dec 15 '13 at 17:48
  • 1
    `cURL` is the answer. cURL allows you to run HTTP Requests from your php code. – Ali Dec 15 '13 at 17:49
  • @Reverb: Are `site1` and `site2` yours? As in, do you have control over both? Or are you trying to create a string generator service that lets others choose and display a string of some arbitrary length? In that case, to generate the random number, you can use an already available function, such as [this one](http://stackoverflow.com/a/4356295/1438393). To retrieve the string in a PHP script, you can simply use [`file_get_contents()`](http://php.net/file_get_contents) or [cURL](http://php.net/curl): `$string = file_get_contents('site1.com/generator.php?leng=5');`. – Amal Murali Dec 15 '13 at 17:58
  • Site 1 is mine, site 2 is someone elses site. – Reverb Dec 15 '13 at 18:23

2 Answers2

1

In second site you can:

$remoteText = file_get_html("http://firstserver.com/page");

And you can handle this var ($remoteText) as you need.

Ignacio Ocampo
  • 2,693
  • 1
  • 20
  • 31
  • What if I wanted to get data let's say like this: $id = "123"; $remoteText = file_get_html("http://firstserver.com/page?id=$id"); And then obviously I know how to do the firstserver code, just not the second server. – Reverb Dec 15 '13 at 17:55
  • 1
    Yep! You can do it. Remember be sepecific with the full url, `http://firstserver.com/page?id=$id` – Ignacio Ocampo Dec 15 '13 at 17:56
  • Cheers, now would you know how I would be able to only allow the display of data from site1 if the IP requesting the data matches the IP allowed ($allowed = "127.0.0.1"; - Just so I can choose who can use this script! – Reverb Dec 15 '13 at 18:03
  • 1
    Yep! It's another question, you should open new question each time. You can restrict your content with IP user address with `$_SERVER['REMOTE_ADDR']` – Ignacio Ocampo Dec 15 '13 at 18:05
0

To get the content of a website you can use AJAX or Curl. Ajax works on the client side and curl on the server side (PHP).

What you asked in the third paragraph can easyly done with both methods. I would prefer AJAX for short strings and Culr for whole websites.

I'm not sure if you mean that... Facebook for example provides a script. Pople can embed this script on their page and get a like button. This script loads another script from facebooks servers.

Powerriegel
  • 595
  • 5
  • 16
  • I think cURL is what I am looking for - In the means that I want to have a string generated (Example: abc123) and I want an external site (site2 in my case) to be able to get this data from the page, in a PHP format. So site2 will regognise site1 string as like it was in the code (IE: $string = we got the string here from site1; – Reverb Dec 15 '13 at 17:54