tags:

views:

70

answers:

3

Suppose you have access to a script which will print or echo an ID string, given a name string, i.e., something like:
http://www.example.com/script.php?name=aNameString
outputing an ID string.

I want to create a script which will allow me to retrieve anIDString, given that I already have a variable holding aNameString, i.e., something like this pseudocode:
$name="Homer Simpson";
$id='www.example.com/script.php?name=$name';

Can you help me understand how I'd do this? ... Thanks, as always!

A: 

Try requiring the file, but remember, you'll need to call the function later.

<?php
  $name = 'Homer Simpson';
  require 'script.php';
?>

That will make the global variable $name, accessible by script.php

However, if it isn't your server, you will need to use a tool like curl to fetch the page.

CodeJoust
+1  A: 

If you are writing code on the same domain, for security reasons you might consider the include() or require() functions instead, and implementing what you need as a function in php. This way, there is no risk to your server being fed rubbish data and crashing your application.

If you need to pull data from another script do so with care, especially a server that isn't trusted. That said, you can do it with either: http://uk.php.net/curl or http://us2.php.net/manual/en/function.file-get-contents.php, the latter of which looks easier to me.

Ninefingers
A: 

In the simplest case, you can use the HTTP wrappers to get the output:

$html = file_get_contents('http://www.example.com/script.php?name=aNameString');

and them take the $html apart, unless you meant something different by "outputing an ID string", it output raw text and not html.

Don