Solved

Testing PHP STD In

Posted on 2011-09-09
5
306 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
ID: 36514538
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 83

Expert Comment

by:Dave Baldwin
ID: 36514742
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
ID: 36515251
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
ID: 36515258
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 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 167 total points
ID: 36521798
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

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
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.

820 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