Forum Moderators: coopster

Message Too Old, No Replies

header call without redirecting browser

         

jackvull

1:38 pm on Jun 27, 2006 (gmt 0)

10+ Year Member



Is there a function similar to a header call that allows a script to call a page without actually outputting anything to the browser?

For example, a script is called from within an iframe, which runs some code on a different server. The second server then needs to call a script on the first server but not output anything to the browser?

I could do this on the first server by reloading the page but don't want to do that because of negative effectes on the user experience.

jackvull

1:44 pm on Jun 27, 2006 (gmt 0)

10+ Year Member



I guess cURL will work...?

jackvull

1:55 pm on Jun 27, 2006 (gmt 0)

10+ Year Member



Next question...
if I use cURL to run a script on a different server.
Is it possible to retrieve a variable value from that script?

Say the script outputs the number 1, can this be retrieved with cURL?

mcavic

3:25 pm on Jun 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



cURL is used to call a Web page, and retreive the output. So if the script outputs the value, then the calling script can see it.

jackvull

3:34 pm on Jun 27, 2006 (gmt 0)

10+ Year Member



I can see how to get the output into a variable:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec();

but not sure how to then get all the stuff in $dat into separate variables.

For example, say the script on the other server outputs:
<html>
<body>
1<br />
25<br />
</body>
</html>

How do I get the 1 and 25 into different variables ignoring all of the header information, etc.
Can't seem to find this in the manual...

coopster

1:25 am on Jun 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What exactly do you need from the external page? Do you need to POST to it or anything else?

Although curl will work, you might certainly be able to use file_get_contents() [php.net] or open a socket and read directly from the page as opposed to invoking curl. Not that curl doesn't work fine, it's just that you can accomplish many simple tasks without it.

jackvull

7:32 am on Jun 28, 2006 (gmt 0)

10+ Year Member



Well, I call the external page and it runs a databse query and then outputs this to the page. Say 2, or 3 field results.
I then need to get these field results into variables from the calling script and use them.
Just not sure how to get the external page field results back into some variables...

coopster

1:10 pm on Jun 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You could use use
file_get_contents()
to retrieve the source of the page into a string, use regular expression matching to locate and store the field values and move them into your local variables.

jackvull

1:23 pm on Jun 28, 2006 (gmt 0)

10+ Year Member



I've been trying this this:

$url = "http://127.0.0.1/htdocs/script.php?ID=".$ID;
$needle = 'Count:';
$contents = file_get_contents($url);
echo $contents;
if(strpos($contents, $needle)!== false) {
echo '<br />found';
} else {
echo '<br />not found';
}

but get a strange error appearing:
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:

Could this be caused by the fact I'm using my localhost or is it likely to be something else?

coopster

4:04 pm on Jun 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



file_get_contents() isn't going to return the headers, it is going to return the entire file into a string. Using your example, it will return something like this in the $contents variable:
<html> 
<head><title>Title</title></head>
<body>
<p>Here is the page you requested!</p>
<p>Here is the Count: 7</p>
</body>
</html>

trillianjedi

4:29 pm on Jun 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not entirely sure what it is you're trying to achieve, but I wonder if an XMLHTTPRequest might suit the task better?

TJ

jackvull

8:34 am on Jun 29, 2006 (gmt 0)

10+ Year Member



unfortunately, that relies on javascript doesn't it?
I need this specific bit of code to run no matter what so worried that people who have javascript disabled might cause it not to run.

I can't figure out what is causing the cgi error though...

eeek

7:40 pm on Jun 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could use use file_get_contents() to retrieve the source of the page into a string

Why not just have curl put the contents into a string in the first place?

coopster

9:37 pm on Jun 30, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



As I said, either will work, but with
file_get_contents()
you can perform in one line of code what will take many lines if using curl.

eeek

11:45 pm on Jun 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, but curl will handle a lot more situations.