Solved

Testing PHP STD In

Posted on 2011-09-09
5
304 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

809 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