C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old April 8th, 2002, 12:42 AM
ElvisSinatra ElvisSinatra is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 0 ElvisSinatra User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
best way to call a PHP script from a C prog?

I know a way to do this, but can anyone (with more knowledge than me of C and PHP) tell me the best way to do it?

I would like my php scripts to parse data and display it as it arrives in chunks from a C program. The C program can use multiple threads to produce the raw data much faster than PHP. But the parsing would be quicker to develop and maintain in PHP than C, using regular expressions, etc.

Example: A form submits to a cgi program written in C. Using multiple threads for speed (which PHP cannot do) the C program simultaneously issues several HTTP requests to different remote servers. As the servers respond, chunks of data become available to be processed (formatted) by a php script and sent to the browser.

Can/should the C program invoke the PHP scripts, or can/should a PHP script oversee the process?

Method 1: the C program spawn/call/run/launch the appropriate php script which formats the data and then either outputs the result or returns it to the C program which outputs it. One way to do this is to HTTP POST the data to the PHP script. However, it seems kind of inefficient to do an HTTP post to the same server the program is on! What about using spawn()? Note: it would be best to take advantage of the PHP compiled with Apache. Is there a way to call a PHP script from a C program and have Apache/PHP parse it rather than invoking the PHP CGI?

Method 2: all of the raw output of the C program returns to the PHP script, the script reads it character by character as it arrives looking for an "end of chunk" marker, parses out a reference to the appropriate php script, and calls that script to format and output the result, continuing until an end of file is reached.

There has to be a better way to do this.

Keep in mind: it would be unreasonable to wait for everything to be processed, formatted, etc. before outputting anything to the screen. That would take far too long. Each chunk needs to be processed by a PHP script and displayed as soon as it becomes available.

Reply With Quote
  #2  
Old March 7th, 2003, 05:35 AM
operator smooth operator smooth is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 61 operator smooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
You should definitely call your program from PHP. Submit your form to a PHP script and use input to construct command line arguments for starting your C program.

You can use popen() call for that, it is pretty much the same as popen() in C.

Is output from your C program serialized so that output from multiple threads doesn't get mixed at random?

To output in chunks, you just process a chunk, format it and echo it to the client. If you use output buffering in PHP, remember to call ob_flush() after printing each chunk.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > best way to call a PHP script from a C prog?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway