Two things:

(1) If all you're doing is outputting, go for php_printf() rather than the
php://stdout or php://output stream.  It avoids the need to open additional
streams and makes sure that your extension won't have problems compiling
against pre-4.3 versions of PHP.

(2) You've got code defined as char[128], and use sizeof() in your
php_stream_write() call which means that you'll wind up outputting the
entire 128 bytes of the char array, plus you don't want the trailing NULL
anyway, so go with strlen() rather than sizeof() here.

That said, the most universal way of retrieving data otherwise output by PHP
is through output buffering.  Recall that in userspace you can do
ob_start(); /* do stuff */ $contents = ob_get_contents(); ob_end_clean(); To
get the output of the /* do stuff */ block into $contents.

In C-land you can take the equivalent approach, or temporarily override the
output handler, but I recommend the ob approach as it's a bit easier.  Take
a look in main/ouput.c for the internal implemetation of the ob_*()
functions.

-Sara

----- Original Message ----- 
From: "Ben Chivers" <[EMAIL PROTECTED]>
Newsgroups: php.internals
To: <[EMAIL PROTECTED]>
Sent: Monday, May 17, 2004 7:07 AM
Subject: Re: Streams - Extensions


> Hi,
>
>  I've been looking at the PHP streams source code.  I wondered if you
could
> help with this problem i've got with a PHP extension.  Any help with this
> would be very much appreciated!
>
>  What I want to do is create a php extension which parses php code to the
> interpreter.  e.g. the php script using the php extension will something
> like this:
>
>  <?php
>
>      passCode("echo('Hello World');");
>
>  ?>
>
>  The extension will look something like this:
>
>  PHP_FUNCTION
>  {
>      char code[128] = "echo('Hello World');";
>      size_t bt;
>
>      php_stream * phpStream = php_stream_open_wrapper("php://stdout", "w",
> REPORT_ERRORS, NULL);
>      if (stream)
>      {
>          zend_printf("Stream Connected");
>          bt = php_stream_write(phpStream, code, sizeof(code));
>          php_stream_close(phpStream);
>      }
>      else
>      {
>          zend_printf("Stream Connection Error");
>      }
>  }
>
>  This function would pass the code "echo('Hello World');" to PHP and
execute
> it.  What I am having problems is, is retrieving the output of this
function
> e.g. "Hello World".  But the problem with PHP is that it's unidirectional
> (oneway) from what I know.
>
>  Is there a way to read the output from PHP?  What would be the best way
of
> doing that?  Would it have anything to do with flushing?
>
>  Thank you very much for your time.  Any help with this would be most
> appreciated.  I wanted to ask you, as you are a developer for PHP, and you
> developed the php streaming for php extensions and c++ programs.
>
>  I hope it makes sense.
>
>  Many Regards,
> Ben Chivers
>
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.686 / Virus Database: 447 - Release Date: 14/05/2004

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to