Solved

Testing PHP STD In

Posted on 2011-09-09
5
303 Views
Last Modified: 2012-05-12

I need to write a php script that accepts input from php://stdin. I've never used stdin before. Ideally I want to create a test where I can submit data in a stream to my script, but I don't even know where to start.

Any ideas?
0
Comment
Question by:cnxmax
5 Comments
 

Author Comment

by:cnxmax
Comment Utility
Here's what I have so far. I need to be able to use the data in $STDIN. I just have no clue how to submit a test with data to my scrip where it will receive the STDIN.
$STDIN = '';
if(!defined("STDIN")) {
define('STDIN', fopen('php://stdin','r'));
}
$STDIN  = fread(STDIN, 694408);

Open in new window

0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
The first question is this going to be a 'command line' program?  Second, is it supposed to 'interactively' accept input from STDIN?
0
 
LVL 13

Accepted Solution

by:
F Igor earned 333 total points
Comment Utility
if you want to pass some file to your script to read its content try:


readfile.php Example:

<?php

$filename=$_SERVER["argv"][0];

echo "Reading file $filename \n";

$data=file_get_contents($filename);


//use the $data variable with file content


?>

Open in new window


And call it:

php readfile.php /path/to/file/to/read.txt

Open in new window





 
0
 
LVL 13

Assisted Solution

by:F Igor
F Igor earned 333 total points
Comment Utility
If you want to pass the input stream from another program via stdin:


File readdata.php
<?php

$data = file_get_contents("php://stdin");


echo "$data";

?>

Open in new window


and call it, for example:

ping 127.0.0.1 | php readdata.php

Open in new window


This call pass the results of ping to the php script.

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 167 total points
Comment Utility
I've used STDIN in email pipe scripts.  See lines 37-51 for an example.  It works like any file with fopen() and fread().  Easy!

HTH, ~Ray
#!/usr/bin/php -q
<?php // /email_pipe/index.php


// THIS IS AN EMAIL PIPE SCRIPT.
// THIS SCRIPT IS STARTED AUTOMATICALLY FOR EACH MESSAGE.
// NOTE THAT THIS SCRIPT IS ABOVE THE /public_html/ DIRECTORY TO PREVENT ACCIDENTAL EXECUTION


// --> HOW DO WE KNOW WHICH EMAIL MESSAGES GET SENT HERE?
// THIS SCRIPT RECEIVES MESSAGES SENT TO email_pipe@your.org
// CREATE AN EMAIL MAILBOX EXCLUSIVELY FOR AUTOMATED PROCESSING.
// SET UP AN EMAIL FORWARD FOR THAT MAILBOX IN cPANEL->EMAIL LIKE THIS:
// 1...5...10...15...20...25...
// |/home/{account}/email_pipe/index.php


// --> WHEN YOU UPLOAD, THIS SCRIPT WILL BE MARKED RW-R-R BUT THAT IS WRONG
// THIS SCRIPT MUST BE MARKED EXECUTABLE x0755
// YOU CAN USE FTP SOFTWARE TO CHMOD TO RWX-RX-RX


// --> NOTE THE FIRST LINE OF THIS SCRIPT MUST SAY #!/usr/bin/php -q STARTING IN COLUMN ONE
// 1...5...10...15...20...25...
// #!/usr/bin/php -q
// <?php ... PROGRAM CODE FOLLOWS


error_reporting(E_ALL);

// USE THE OUTPUT BUFFER - THIS DOES NOT HAVE BROWSER OUTPUT
ob_start();

// COLLECT THE INFORMATION HERE
$raw_email = NULL;

// TRY TO READ THE EMAIL FROM STDIN
if (!$stdin = fopen("php://stdin", "R"))
{
    echo "ERROR: UNABLE TO OPEN php://stdin " . PHP_EOL;
}

// ABLE TO READ THE MAIL
else
{
    while (!feof($stdin))
    {
        $raw_email .= fread($stdin, 4096);
    }
    fclose($stdin);
}


// REMOVE MULTIPLE BLANKS - AND OTHER PROCESSING AS MIGHT BE NEEDED
$raw_email = preg_replace('/ +/', ' ', $raw_email);

// SPEW WHAT WE GOT, IF ANYTHING, INTO THE OUTPUT BUFFER
var_dump($raw_email);

// CAPTURE THE OUTPUT BUFFER AND SEND IT TO SOMEONE ELSE VIA EMAIL
$buf = ob_get_contents();
mail ('you@your.org', 'EMAIL PIPE DATA', $buf);
ob_end_clean();

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
The viewer will learn how to dynamically set the form action using jQuery.
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…

728 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