By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,567 Members | 1,592 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,567 IT Pros & Developers. It's quick & easy.

Accessing a URL from a PHP Script

P: n/a
Is there any way to access a URL from within my PHP CLI script?
Basically, I want to use my script to "hit" a web page on a remote
server. I then intend for that remote server to perform an action
based on the configuration for that page.

How do I initiate a call to the remote server's URL from within PHP?

Thanks,
justbn

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
I have made some progress but still have a problem. I can do this
using the following :

<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

Unfortunately, this does not seem to work when the php script is
executed from the command line. When I call this script via a browser,
it successfully sends the header.

Any idea how to do this from the command line?

Thanks,
justbn

Jul 17 '05 #2

P: n/a
try Curl or fopen

$fp = fopen("http://www.example.org/path/to/file.html", "r");

curl is tricky, and you need to compile it in. If your just trying to
"hit" a web page, fopen should be fine.

http://us4.php.net/manual/en/function.fopen.php

Jul 17 '05 #3

P: n/a
NC
justbn wrote:

Is there any way to access a URL from within my PHP CLI script?
Basically, I want to use my script to "hit" a web page on a remote
server. I then intend for that remote server to perform an action
based on the configuration for that page.

How do I initiate a call to the remote server's URL from within PHP?


$contents = file_get_contents('http://www.example.com/');

Cheers,
NC

Jul 17 '05 #4

P: n/a
NC and Hacka,

Thanks for the ideas. Both of these worked perfectly.

justin

Jul 17 '05 #5

P: n/a
justbn wrote:
NC and Hacka,

Thanks for the ideas. Both of these worked perfectly.

justin

If you want to easily get not only the HTML, but all of the linked CSS,
JS, images, etc. you can have PHP point to wget, which as pretty robust
page and site mirroring capabilities. Below is a script and Windows .bat
file I use to bookmark/save sites remotely. There's a link on my browser
bookmark bar that sends the current URL to this script, which saves a
copy of the URL and logs the entry. It's helped me be able to have
cached copies of good information that goes away 6 months later and
given me file-based searching of all of the content in my bookmarks.
fetch.bat should all be one line.

-------------------------------------
J Wynia
Myriad Intellect, Inc.
"Web technology that earns its keep."
www.myriadintellect.com
-------------------------------------

--------addbookmark.php---------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<script language="JavaScript">
<!--
history.back();
//-->
</script>
</head>

<body>

<?php
$url = $_GET['url'];

system("pathto/fetch.bat \"$url\"");

$filename = 'bookmarks.list';

if($url){
$url = urldecode($url);

$content = ("$url\n");
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
die("Cannot open file ($filename)");
exit;
}

if (fwrite($handle, $content) === FALSE) {
die("Cannot write file ($filename)");
exit;
}

fclose($handle);

} else {
die("Cannot write file ($filename)");
}
print("$url added");
} else {
print("No URL");
}

print("<hr><pre>");

?>
</body>
</html>
--------addbookmark.php---------------

--------fetch.bat---------------
pathto/wget.exe -p --convert-links -P pathwheretosave/sites/ %1
--------fetch.bat---------------
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.