QNetworkAccessManager with PHP and MYSQL



  • Hi all Qt'ers,
    I am trying to use QNetworkAccessManager to get data from php Internet page that shows data from MYSQL base. Unfortunately when I use readAll() function for QNetworkReply object I get the whole QByteArray object starting from "<!DOCTYPE html>..." Is there any possibility to get only data from MYSQL base by calling another function or in other way?

    Kind Regards,
    Werter



  • Hi @werter,

    I am trying to use QNetworkAccessManager to get data from php Internet page that shows data from MYSQL base. ... Is there any possibility to get only data from MYSQL base by calling another function or in other way?

    Do you have control over the PHP script?

    If yes, then I'd suggest you modify the PHP page to return non-HTML, such as JSON, using something like print json_encode($rows) (I'm happy to help with that, though obviously its not really a Qt thing ;)

    If no, then you'll need to use QString and friends to parse the HTML... in that case, show us a bit more of the HTML, and we can probably help get you started there too :)

    Cheers.



  • Hi @Paul-Colby ,
    Thank you for your rapid answer. I have an opportunity to write and change PHP script however the server is not mine. I have changed as You suggested and used 'print json_encode($row)' in PHP script but it did not help. The results on the internet page are displayed in JSON style but the result in Qt is the same. Large QByteArray object with 'doctype html' first.

    Cheers.


  • Lifetime Qt Champion

    Hi,

    What does your server return exactly ? Do you have a REST endpoint that returns the json data generated from your database ?



  • @werter said:

    I have an opportunity to write and change PHP script

    Great :)

    I have changed as You suggested and used 'print json_encode($row)' in PHP script but it did not help. The results on the internet page are displayed in JSON style but the result in Qt is the same. Large QByteArray object with 'doctype html' first.

    Ok, so the first challenge is to get rid of that doc type.

    Are you able to share a complete (cut down) PHP script? You'll probably want to:

    • add an explicit content-type header, and
    • make sure there's nothing printing anything prior to that header being set.

    I'll see if I can knock up a near-trivial example later today, but I'm wondering if the hosting service might be forcibly adding the doctype on your behalf.

    Once the content is being sent a pure JSON, then in your Qt code, you can simply do: QJsonDocument::fromJson(/*QByteArray*/).

    Cheers.



  • I'll see if I can knock up a near-trivial example later today

    Here's a minimal PHP example:

    <?php
    $db = mysql_connect ('hostname', 'username', 'password');
    $res = mysql_query('SELECT * FROM mysql.time_zone_name LIMIT 5');
    while ($row = mysql_fetch_assoc($res))
        $rows[] = $row;
    
    header('Content-Type: application/json');
    print json_encode($rows);
    

    Output using curl, with verbose output to show the headers:

    curl http://testhost/json.php -v
    ...
     HTTP/1.1 200 OK
    < Date: Mon, 18 Apr 2016 23:15:05 GMT
    < Server: Apache
    < Vary: Accept-Encoding
    < Content-Length: 232
    < Connection: close
    < Content-Type: application/json
    Closing connection #0
    [{"Name":"Africa\/Abidjan","Time_zone_id":"1"},{"Name":"Africa\/Accra","Time_zone_id":"2"},{"Name":"Africa\/Addis_Ababa","Time_zone_id":"3"},{"Name":"Africa\/Algiers","Time_zone_id":"4"},{"Name":"Africa\/Asmara","Time_zone_id":"5"}]
    

    I hope that helps.

    Cheers.



  • @SGaist said:

    Hi,

    What does your server return exactly ? Do you have a REST endpoint that returns the json data generated from your database ?

    Sorry, but I do not know how to check it. The answer was displayed like this:
    {"Index":"1","Name":"aaa","Surname":"AAA","Phone":"111111111"}{"Index":"2","Name":"bbb","Surname":"BBB","Phone":"222222222"}



  • @Paul-Colby said:

    header('Content-Type: application/json');

    Using this option above in PHP code I get much less QByteArray object:
    <!DOCTYPE html>
    <html>
    <body>

    {"Index":"1","Name":"aaa","Surname":"AAA","Phone":"111111111"}{"Index":"2","Name":"bbb","Surname":"BBB","Phone":"222222222"}
    </body>
    </html>

    However I would like to have an opportunity for managing my database on my page: for example display the results for specified options. I did this as an example - displayed all the results from base on my page. Shall I put this piece of php code in any other file (not index.php like now) to do so?


  • Lifetime Qt Champion

    If you don't want to have several backends to access your data, you should separate the data source(s) and the rendering part. Think model/view, write one/several web service(s) to access your database and use that to render your website. You can then use the same service from your application.



  • @SGaist said:

    If you don't want to have several backends to access your data, you should separate the data source(s) and the rendering part. Think model/view, write one/several web service(s) to access your database and use that do render your website. You can then use the same service from your application.

    Is something I can follow, Internet page tutorial or yt video?

    @Paul-Colby,
    And if I get this whole QByteArray can you point out something more than "QString and friends to parse HTML"?

    I do not have large pression to use QNetworkAccessManager. Maybe I could invoke an Android method from Qt? Would not it be easier? I wonder about it because I would like to save phone number from Internet database on my smartphone.


  • Lifetime Qt Champion

    It's not the only way to do it but AngularJS shows the idea pretty well.



  • Thank you. I take a look on it.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.