Solved

Sending file_get_contents to dev/null

Posted on 2009-05-15
3
358 Views
Last Modified: 2012-05-07
Using PHP I need to access a remote website so it can do something (sort of like a crontab)

This works:
$devNull = file_get_contents('http://www.example.com/update.php');

But it places the contents of the website in a string.  I don't want that, it is a watse of memory.

How can I access the webpage but not collect its output?
0
Comment
Question by:hankknight
3 Comments
 
LVL 2

Accepted Solution

by:
crash36936 earned 300 total points
ID: 24398962
I guess you just need to see if the site is up?

You can try this: http://us.php.net/manual/en/function.get-headers.php

More information, without all the extra baggage
0
 
LVL 7

Assisted Solution

by:ycTIN
ycTIN earned 100 total points
ID: 24401094

$target = "http://www.example.com/update.php";

$f = fopen($target, "r");

fclose($f);

Open in new window

0
 
LVL 3

Assisted Solution

by:markh789
markh789 earned 100 total points
ID: 24409488
Attched is some code, the string $header_data becomes an array, here is what it should contain:
Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Date: Sat, 29 May 2004 12:28:13 GMT
    [2] => Server: Apache/1.3.27 (Unix)  (Red-Hat/Linux)
    [3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
    [4] => ETag: "3f80f-1b6-3e1cb03b"
    [5] => Accept-Ranges: bytes
    [6] => Content-Length: 438
    [7] => Connection: close
    [8] => Content-Type: text/html
)

OR

Array
(
    [0] => HTTP/1.1 200 OK
    [Date] => Sat, 29 May 2004 12:28:14 GMT
    [Server] => Apache/1.3.27 (Unix)  (Red-Hat/Linux)
    [Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
    [ETag] => "3f80f-1b6-3e1cb03b"
    [Accept-Ranges] => bytes
    [Content-Length] => 438
    [Connection] => close
    [Content-Type] => text/html
)

You can check $header_data[0] for "200 OK" using strstr http://us.php.net/manual/en/function.strstr.php

<?php

$header_data = get_headers("http://www.example.com/update.php");
 

if(strstr($header_data[0], "200 OK")) {

 echo ":) - sweet.";

} else {

 echo ":( - fail";

}

?>

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now