473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Accessing a URL from a PHP Script

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
5 12597
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
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
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
NC and Hacka,

Thanks for the ideas. Both of these worked perfectly.

justin

Jul 17 '05 #5
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ms_chika | last post by:
Hi to all, I have this problem in xsl wherein i want to access a variable in javascript and use it my xsl. How would i access or use a javscript variable in my xsl file? Please help. ...
3
by: Christopher Benson-Manica | last post by:
I appreciate all the responses to my earlier post about accessing named elements. However, I'm still wondering about my actual problem, which is that I need to initialize some arrays of named...
6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
5
by: Vinod Kumar | last post by:
Hi All, Can I access a Javascript function written in an ASP.NET from the codebehind (C#) of that page? If yes, kindly give the code snippet to do this. Thanks and Regards Vinod Kumar
7
by: Chuck Anderson | last post by:
I'm pretty much a JavaScript novice. I'm good at learning by example and changing those examples to suit my needs. That said .... ..... I have some select fields in a form I created for a...
3
by: kosmodisk | last post by:
Hi, I'm having problem accessing javascript-created elements from opened window. This occurs only when I'm including another files in opened window, javascript or css. When I comment out...
0
by: R. Rajesh Jeba Anbiah | last post by:
I have posted a feature request http://bugs.php.net/40853 as there is no reliable method to get current URI, accessing script, etc ($_SERVER is just a variable and prone to tampering). I hope,...
1
by: leenadiwan | last post by:
Friends, Im having problem with accessing a JS file placed in another folder from a JSP popup. I tried all the following: <SCRIPT LANGUAGE="JavaScript1.1" ...
2
by: VasudevanT | last post by:
How to Access a script values in jsp
51
by: Ojas | last post by:
Hi!, I just out of curiosity want to know how top detect the client side application under which the script is getting run. I mean to ask the how to know whether the script is running under...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.