472,805 Members | 2,135 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 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 12048
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{

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.