Search code examples
phpc++xmlqtqnetworkaccessmanager

QT Network reply won't give me back result from PHP script


Hi all I have one problem. I am connecting QT with php script..Everything works fine, but can somone help me how to receive php result from it. here is my code :

void MainWindow::Dodaj_korisnika(QUrl url)
{
    //reply = qnam.get(QNetworkRequest(url));
    ////////////////////////////////////////////////////
    QUrl params;
    params.addQueryItem("action","Dodaj_korisnika");
    params.addQueryItem("ime",ui->lineEdit);
    params.addQueryItem("prezime",ui->lineEdit_2);
    params.addQueryItem("broj",ui->lineEdit_3);
    params.addQueryItem("adresa",ui->lineEdit_4);

        QByteArray data;
        data.append(params.toString());
        data.remove(0,1);

        QNetworkRequest request;
        request.setUrl(url);
        request.setHeader(QNetworkRequest::ContentTypeHeader,
        QVariant("application/x-www-form-urlencoded"));
        QNetworkAccessManager *manager = new QNetworkAccessManager(this);
        QNetworkReply *reply = manager->post(request, data);
        connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
}

And I have found on forums that I need to use this function:

void MainWindow::replyFinished(QNetworkReply *reply)
{
    QString data = reply->readAll().trimmed();
    // document.setContent(reply);
}

But ofc I am not getting anything into this function..Is it possible to read from QT manager variable? I am receiving xml result. And I want to put it as QDomDocument is that possible? Please give me some hint or some example if you have some time.. Thank's all!


Solution

  • Are you sure the PHP script received something?

    • First you are creating a signal and slot each time you are posting
    • Second you create the connection after posting
    • Third verify that your query is well formed and that the php script is receiving it. If you have the script log\print incoming messages

    Edit: This is How it should look like

    // early in the code, like in the constructor 
    void MainWindow::init(){
      this->manager = new QNetworkAccessManager(this);
      connect(this->manager, SIGNAL(finished(QNetworkReply*)), 
              this, SLOT(replyFinished(QNetworkReply*)));
    }
    
    void MainWindow::Dodaj_korisnika(QUrl url){
    
        QUrl params;
        params.addQueryItem("action","Dodaj_korisnika");
        params.addQueryItem("ime",ui->lineEdit);
        params.addQueryItem("prezime",ui->lineEdit_2);
        params.addQueryItem("broj",ui->lineEdit_3);
        params.addQueryItem("adresa",ui->lineEdit_4);
    
        QByteArray data;
        data.append(params.toString());
        data.remove(0,1);
    
        QNetworkRequest request;
        request.setUrl(url);
        request.setHeader(QNetworkRequest::ContentTypeHeader,
        QVariant("application/x-www-form-urlencoded"));
    
        //Here you don't need to get the reply, it will be the same given to 
        //MainWindow::replyFinished
        this->manager->post(request, data);
    
    }
    
    void MainWindow::replyFinished(QNetworkReply *reply){
        //Use the reply as you wish
    }