SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to integrate PHP and C program

    Hello all..

    Let me put my problem in a simple way. I have written a C program, which i will keep in my server. Now i am developing a
    website using PHP, so that other users can also have access to my stand alone C program.

    So here i want my PHP to take input from users and bring that input and run my C program and then again take the results of
    the C program back to the users using PHP(probably using the same website itself).

    Please suggest me how to do it. It would be easier for me to understand if you can tell me using simple program.
    For example my C program has a function that adds two numbers. The users can provide their input (the numbers) using my
    website. Then PHP will somehow interact with C function and return the results to the user.

  2. #2
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I realize it's probably something strange, but here is what I have.

    I have an application written in C/C++. This application has Perl wrapper which was made by application's authors using SWIG. My website is written in PHP, so I'm looking for some ways to make PHP work with C/C++ application.

    The only way I can think of now is to create a CGI script (perl script) which accepts POST request from my website (AJAX request), sends it to the recognition engine through it's Perl wrapper, gets the required data and returns the required data as a response to AJAX request.

    Do you think it could be done this way? Are there any better solutions?

  3. #3
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    699
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    Not all that strange. Just use the php exec command. I'm assuming you have access to the actual C executable.
    http://www.php.net/manual/en/function.exec.php

  4. #4
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you..
    i have read the exec command, but still have some problems understanding it.
    exec(command,return_value)
    How can we specify the command line argument of C here.

    If my C pgm is to compute the sum of two numbers
    Can i write exec("myPgm 3,4", $answer)

    If you have some time to spare, can you pls write a small C pgm for this.
    And how am i going to get the return value from C pgm to PHP

  5. #5
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ahundiak View Post
    Not all that strange. Just use the php exec command. I'm assuming you have access to the actual C executable.
    http://www.php.net/manual/en/function.exec.php
    Yes i have access to the actual C executable. But still i dont know how to write the exec command.
    If my C program's name is myPgm and it calculates sum of 2 numbers. Can i write exec command as:

    exec("myPgm 3 5", $answer)

    let me write a sample C pgm

    int main(int a, int b)
    {
    int sum= a+b;
    return sum;
    }


    If this is my C program how can i run it in commandline using PHP exec and how am i going to get the return value.

    Please help me. I am not very skilled, so please explain in simple terms.

  6. #6
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ahundiak View Post
    Not all that strange. Just use the php exec command. I'm assuming you have access to the actual C executable.
    http://www.php.net/manual/en/function.exec.php
    Or use this C program

    #include <stdio.h>

    int main(int argc, char *argv[]) {
    if ( argc != 3) {
    printf("Usage:\n %s Integer1 Integer2\n",argv[0]);
    }
    else {
    printf("%s + %s = %d\n",argv[1],argv[2], atoi(argv[1])+atoi(argv[2]));
    }
    return 0;
    }

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Just be -VERY VERY- careful about letting users pass info into the shell! Great way to get your system hacked.

    escapeshellarg and possibly escapeshellcmd

  8. #8
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)
    Another option, if you are REALLY brave, is to write a PHP socket extension to expose the functionality of your C program to PHP as native PHP functions.

  9. #9
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi did you got solution for your problem. me too having same problem can you help me.

  10. #10
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,290
    Mentioned
    122 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by kubendran88 View Post
    hi did you got solution for your problem. me too having same problem can you help me.
    Did you READ the whole thread? There are a number of responses with suggestions to follow.

    Please read the thread, attempt to implement the suggested changes. If you still have questions, please create a new thread.

    THREAD CLOSED
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •