Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Testing PHP STD In

Posted on 2011-09-09
5
Medium Priority
?
328 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 84

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 1332 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 1332 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 668 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
Suggested Courses

916 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