次のコードでは、ポスト リクエストに応答してリモート サーバー上の php スクリプトから変数でデータを取得しようとしています。
私はバグを見つけるのに非常に多くの時間を費やしましたが、できませんでした。これは、非 oop 環境で完全に機能する同じコードです。送信された 1 セットのポスト データに対して、返されるデータ (std::string) は常に同じです。
HTML形式でスクリプトを確認しましたが、問題はありません。したがって、書き込みコードに何か問題があると推測しています。
OOP 環境で C++ と libCurl を使用しています。
#include "Server.h"
namespace model
{
// private:
Server::Server ( )
{
curl_global_init ( CURL_GLOBAL_ALL );
curl = curl_easy_init ( ) ;
};
Server* Server::singleton = NULL;
// public:
Server* Server::Instance ( )
{
if ( !singleton ) singleton = new Server ( );
return singleton;
};
Server::~Server ( )
{
curl_easy_cleanup ( curl );
curl_global_cleanup ( );
};
std::string Server::readScript ( std::string scriptAddress, std::string postData )
{
std::string response = "-1";
if ( curl )
{
curl_easy_setopt ( curl, CURLOPT_URL, scriptAddress.c_str ( ) );
curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, &Server::writeString );
curl_easy_setopt ( curl, CURLOPT_WRITEDATA, &response );
if ( ! postData.empty ( ) )
{
curl_easy_setopt ( curl, CURLOPT_POSTFIELDS, postData.c_str ( ) );
}
curl_easy_setopt ( curl, CURLOPT_CONNECTTIMEOUT, 10L );
CURLcode cc = curl_easy_perform ( curl );
if ( cc == CURLE_OPERATION_TIMEDOUT ) MessageBox ( 0, "The operation timed out.\nPlease try again later.", "Error!", MB_OK | MB_ICONEXCLAMATION );
else if ( cc == CURLE_COULDNT_CONNECT ) MessageBox ( 0, "Couldn't conect to the server.\nPlease try again later.", "Error!", MB_OK | MB_ICONEXCLAMATION );
else if ( cc == CURLE_REMOTE_ACCESS_DENIED ) MessageBox ( 0, "Access is denied.\nPlease contact our support team.", "Error!", MB_OK | MB_ICONEXCLAMATION );
else if ( cc == CURLE_GOT_NOTHING ) MessageBox ( 0, "Server did not return anything.\nPlease try again later, if the problem persist please contact our support team.", "Error!", MB_OK | MB_ICONEXCLAMATION );
else if ( cc == CURLE_REMOTE_FILE_NOT_FOUND ) MessageBox ( 0, "Couldn't find resource.\nPlease try again later, if the problem persist please contact our support team.", "Error!", MB_OK | MB_ICONEXCLAMATION );
else if ( cc == CURLE_FAILED_INIT ) MessageBox ( 0, "The initialization failed.\nPlease contact our support team.", "Error!", MB_OK | MB_ICONEXCLAMATION );
else if ( cc == CURLE_COULDNT_RESOLVE_HOST ) MessageBox ( 0, "Couldn't resolve host.\nPlease contact our support team.", "Error!", MB_OK | MB_ICONEXCLAMATION );
else if ( cc == CURLE_WRITE_ERROR ) MessageBox ( 0, "Response write error.\nPlease contact our support team.", "Error!", MB_OK | MB_ICONEXCLAMATION );
else if ( cc == CURLE_TOO_MANY_REDIRECTS ) MessageBox ( 0, "Droping connection, due to too many redirects.\nPlease contact our support team.", "Error!", MB_OK | MB_ICONEXCLAMATION );
else if ( cc == CURLE_SEND_ERROR ) MessageBox ( 0, "Failed sending data to the server.\nPlease try again later, if the problem persist please contact our support team.", "Error!", MB_OK | MB_ICONEXCLAMATION );
else if ( cc == CURLE_REMOTE_FILE_NOT_FOUND ) MessageBox ( 0, "Remote server could not be found.\nPlease try again later, if the problem persist please contact our support team.", "Error!", MB_OK | MB_ICONEXCLAMATION );
MessageBox( 0, response.c_str(), "response", 0 );
}
else
{
MessageBox ( 0, "Something went wrong.\nPlease try again later.", "Error!", MB_OK | MB_ICONEXCLAMATION );
}
return response;
};
/** private static function */
/** -------------------------------------------------------------------------------------------------------------------------------- */
/** -------------------------------------------------------------------------------------------------------------------------------- */
int Server::writeString ( void *ptr, int size, int count, void *stream )
{
( ( std::string* ) stream )->append ( ( char* ) ptr, 0, size*count );
return size*count;
}
};// end of namespace model
編集: 0が 予想される場合は-10を返し、3が予想される場合は-13を返し、1が予想される場合は-11を返します。