Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Testing PHP STD In

Posted on 2011-09-09
5
Medium Priority
?
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

722 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