Thread: PHP to C++ Communication

  1. #1
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034

    Smile PHP to C++ Communication

    Hey guys, I've got a small PHP script which opens a socket to my server (C++ program). What would be the best way to communication between PHP/C++ sockets? JSON? XML? boost::serialize? An API for PHP with an equivalent parsing library for C++? custom?

    At the moment I'm just sending a message code (say 100, 101) then a string (say "hello") and using: (eg. "101hello")

    Code:
    class Message
    {
    	public: unsigned int ID;
    	public: std::string Data;
    };
    
    
    //inside server class
    private: boost::asio::streambuf data;
    
    
    //inside server class read function
    std::istream data_stream(&this->data);
    
    Message* message = new Message;
    
    data_stream >> message->ID;
    data_stream >> message->Data;
    However if the data string starts with a digit, the stream sends them to the ID variable instead of Data. If there's a fix for that problem (some kind of separator? such as "101-hello-500-zzz" would be 101, "hello", 500, "zzz") then I could survive using this method. I could quickfix that, but I would like other ideas.

    Otherwise can anyone recommend a C or C++ XML library or otherwise?

    Thanks in advance
    Last edited by Dae; 07-27-2009 at 12:09 AM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I would say JSON. It's simple to parse and use. You can't use Boost.Serialization for inter-language transfer; it's too bound to C++. (Someone recently tried - there was a long thread on the Boost mailing list.) XML is, in my opinion, overkill for most things.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    What is the data? How is it represented (Plain text with spaces, without spaces, etc)? Is there a need to use a generic communication protocol?

    For example if each message ends with a particular character then you could use getline with that character as the terminator.

  4. #4
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Thanks for the info. I tried JSON and normally I have no problems, but it seems PHP's json_decode(), as well as jQuery's getJSON(), as well as JavaScript's eval() functions fail to decode the "data" tag (shown in the xml below). Apparently the forward slashes (/) have to be escaped (\/) and even after doing so I had no luck. It was weird because if I manually copy and paste the JSON into the script, it works fine, but as a string variable, it fails. Whatever. I decided to try boost::serialize with boost XML archive. XML works fine (even before using boost). It works well except for the bloating, and you must declare all tags and sometimes values, or else it throws exceptions (stream/undefined/etc).

    Here's how it's represented via boost::serialize:

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <!DOCTYPE boost_serialization>
    <boost_serialization signature="serialization::archive" version="5">
    <image class_id="0" tracking_level="0" version="0">
            <id>5</id>
            <format>image/gif</format>
            <command>get</command>
            <data>/9j/4AAQSkZJRgABAQAAAQABAAD//gATYjQ0OGUyYjgwNzg3ZWE2MwD/2wBDAAgG
    BgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAx
    NDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIy
    MjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCABGAMgDASIA
    AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgED
    AwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2Jy
    ggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1
    dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJ
    ytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAA
    AAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJB
    UQdhcRMiMoEIFEKRobHBCSMzUvA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pc/QdqdPLaspe/pHvoKWKilpqz6F
    t7u3u1ZraeKZVO1jG4YA+nHepq4LwbeeIM2umJ4aTSNKgT5nuCxc/Tplie+PX6V3
    tcVan7OXL/X4G8JcyuFFFFZFjh0ooHSigAooooAaetVNUnltdJvLiBd00UDugxnL
    BSR+tWz1pkrrHE7sCVVSSAM8U1uJ7Hinwr1DSIta1C71i5hS/cAwy3DAdSd5BPc8
    V6pe+LvD9hA0s+r2eAM7UlDsfoBk14ZNf+G9Z8Tyz31nJpenEsdtmu52OeM5OB+A
    rpbC2+FQkBkurx/a48wD/wAdAr3MVhozn7Sal6JXOGjVcY8qaOdt4bjxZ8QHuNFt
    mhSS5EoIGBEoI+Y46dM/jVj4p6h9t8b3EQbKWsaQD643H9WNejyeNfB3hfToBpaw
    yQSNgpZBdwwOrAkH868S1O9/tXXLq9dtgubhpCW/hDNn9K6cM5VanO42jFWV9/60
    MqqUY8qd23qd/P4P8PDw7pDat4hXT7oWoYwna33iXzt6/wAWPwrqbJvhrNaRWgk0
    dyiBC8qiNmwMZLHHNeRaE+n3vie3fxBM5s5JCZ3JPPHGSOQM4r1TVLH4ZWmnNM0d
    jNhfkjtrhmkc9gNrZrnxMHFqEpSbeumxpSkmm0l8zz7xKmkaB453aP5VxYxFJDHu
    EiEkZK55yP8AGtj4beE4PEQ1TUL62imjRTFCkmVQynnPy+nH/fVcBMA7vNHEY4Gk
    IRc5x3xnvgYr3HQNR0/wn8LIbqO4gab7OZtocZeVugx7EgfQVvi3OlRUYNuTsrmd
    FKU25bLU8lHhi8n8WN4etpIZ7pZTGXQnYCBlucZwMH8q6ix17xpZa9DoenahFqb2
    wCeRBGvlKFGMFto4HGefxrL0eafRvCmqeIyzfbb6X7FbSnqM/NI/1wAM+ua1fB3g
    fxFeaQmq6Xra6el1kEI7hyFJHOPxqq048rdVqy01XXqKEXdct77/ACOpl0P4lXym
    SXX7O1J5EUXGPbIX+prjpfGni7wtrklhqd4LvymAlilAdWB9DjPSut/4Vx4mnP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pyHrt2lh9NoP866jRPC6WGmSW+qXDatPM26aW6G4N6AA5wBXQUVzTxMp
    KySS8lY1jSSd3r6mbb+H9GtHD2+k2MTj+JLdAfzxV6aCG4iMU0SSRnqjqCD+BqSi
    sXKTd2y0ktivb2FnZ/8AHtaQQ/8AXOML/KrFFFJtvcYUUUUgCiiigBw6UUDpRQAU
    UUUAJijFFFABgUYFFFABijAoooAMCjFFFABgUYFFFABgUYoooAMCjAoooAMUYFFF
    ABgUYoooAMCjAoooAMCjAoooAWiiigD/2Q==
    </data>
    </image>
    </boost_serialization>
    id/format/command/data are the important variables. Command variable tells C++ what PHP wants. C++ fills in data variable, sends it to PHP, PHP sends it to page via AJAX, AJAX changes the data variable and sends it back to PHP via AJAX, and PHP sends it back to C++ for finalization. Sometimes certain tags aren't necessarily used, but boost requires they be defined, so I just null them out before sending back and forth then resync afterwards.

    That's just the basic idea. I may expand on it tomorrow. G'night.
    Last edited by Dae; 07-27-2009 at 09:51 AM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unkown Hang
    By Bladactania in forum C Programming
    Replies: 31
    Last Post: 04-22-2009, 09:33 AM
  2. PHP installation
    By ssharish2005 in forum Tech Board
    Replies: 8
    Last Post: 11-23-2007, 09:42 PM
  3. Looking for communication lib
    By BrownB in forum C Programming
    Replies: 3
    Last Post: 04-27-2005, 10:01 AM
  4. PHP on my Computer!
    By xxxrugby in forum Tech Board
    Replies: 4
    Last Post: 03-15-2005, 09:34 AM
  5. C++ and PHP communication
    By Korhedron in forum Game Programming
    Replies: 4
    Last Post: 01-12-2004, 06:37 AM