Sunday, September 4, 2022

[SOLVED] Curl - checking connection

Issue

I used Curl 7.2.9 and checked connection this way:

Here's example:

curl = curl_easy_init();
bool result = false;
if(curl)
{
    curl_easy_setopt(curl, CURLOPT_URL, m_checkConnectionUrl);
    CURLcode res = curl_easy_perform(curl);
}
if(res != CURLE_OK)
{

}
else
{
    // connection is available
}

Now I switched to curl-7.33.0 and got *CURLE_WRITE_ERROR* error, and to make it work I must code it like

std::string output;
char* encodedUrl = curl_easy_escape(curl, m_checkConnectionUrl, 0);
curl_easy_setopt(curl, CURLOPT_POST, 0);
curl_easy_setopt(curl, CURLOPT_URL, encodedUrl);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeMemoryCurlCallbackStub);
CURLcode res = curl_easy_perform(curl);

But I don't need to write anything. Any ideas?


Solution

Manily the Curl option *CURLOPT_WRITEFUNCTION* is used to have a certain amount of data periodically(at the callback functoin) to handle a large file download. I don't see any reason to use this with your curl purpose, regardless the version.

Remove the *CURLOPT_POST*(by default its 0) and *CURLOPT_WRITEFUNCTION* from the code and it should work. If it doesn't, then you are doing something wrong at other places in your code!

Also, if you are checking whether the URL is ok or not, then using CURL is ok. But to only check for connection, you can only check whether the port 80 of the domain is on or not.



Answered By - Sabuj Hassan
Answer Checked By - Marie Seifert (WPSolving Admin)