Sending file_get_contents to dev/null

Posted on 2009-05-15
Medium Priority
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?
Question by:hankknight

Accepted Solution

crash36936 earned 1200 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

Assisted Solution

ycTIN earned 400 total points
ID: 24401094

$target = "http://www.example.com/update.php";
$f = fopen($target, "r");

Open in new window


Assisted Solution

markh789 earned 400 total points
ID: 24409488
Attched is some code, the string $header_data becomes an array, here is what it should contain:
    [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


    [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 

$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


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

607 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