0

I am having a strange issue with the following function that I have written. Briefly, this function connects to a URL and fetches the output by using curl functions. However, when I execute this function, I get a "server unexpectedly dropped the connection" message:

/**
 * Connects to remote URL and posts parameters, returns the result
 *
 * @param string $URL 
 * @param string $ArrayPostParameters 
 * @param string $HTTPRequestType 
 * @param string $HTTPAuth 
 * @param string $HTTPAuthUsername 
 * @param string $HTTPAuthPassword 
 * @param string $ConnectTimeOutSeconds 
 * @param string $ReturnHeaders 
 * @return array
 * @author xxxxxxxxxx
 */
function DataPostToRemoteURL($URL, $ArrayPostParameters, $HTTPRequestType = 'POST', $HTTPAuth = false, $HTTPAuthUsername = '', $HTTPAuthPassword = '', $ConnectTimeOutSeconds = 1, $ReturnHeaders = false)
    {
    $PostParameters = implode('&', $ArrayPostParameters);

    $CurlHandler = curl_init();
        curl_setopt($CurlHandler, CURLOPT_URL, $URL);

        if ($HTTPRequestType == 'GET')
            {
            curl_setopt($CurlHandler, CURLOPT_HTTPGET, true);
            }
        elseif ($HTTPRequestType == 'PUT')
            {
            curl_setopt($CurlHandler, CURLOPT_PUT, true);
            }
        elseif ($HTTPRequestType == 'DELETE')
            {
            curl_setopt($CurlHandler, CURLOPT_CUSTOMREQUEST, 'DELETE');
            }
        else
            {
            curl_setopt($CurlHandler, CURLOPT_POST, true);
            curl_setopt($CurlHandler, CURLOPT_POSTFIELDS, $PostParameters);
            }

        curl_setopt($CurlHandler, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($CurlHandler, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($CurlHandler, CURLOPT_FOLLOWLOCATION, 5);
        curl_setopt($CurlHandler, CURLOPT_CONNECTTIMEOUT, $ConnectTimeOutSeconds);
        curl_setopt($CurlHandler, CURLOPT_TIMEOUT, $ConnectTimeOutSeconds);
        curl_setopt($CurlHandler, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3');

        // The option doesn't work with safe mode or when open_basedir is set.
        if ((ini_get('safe_mode') != false) && (ini_get('open_basedir') != false))
            {
            curl_setopt($CurlHandler, CURLOPT_FOLLOWLOCATION, true);
            }

        if ($ReturnHeaders == true)
            {
            curl_setopt($CurlHandler, CURLOPT_HEADER, true);
            }
        else
            {
            curl_setopt($CurlHandler, CURLOPT_HEADER, false);
            }

        if ($HTTPAuth == true)
            {
            curl_setopt($CurlHandler, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($CurlHandler, CURLOPT_USERPWD, $HTTPAuthUsername.':'.$HTTPAuthPassword);
            }

        $RemoteContent = curl_exec($CurlHandler);

    if (curl_error($CurlHandler) != '')
        {
        return array(false, curl_error($CurlHandler));
        }

    curl_close($CurlHandler);   

    return array(true, $RemoteContent);
    }

When I debugged the function, I noticed that curl_exec() function causes this issue. No curl_error message and no PHP error is generated.

Do you have any idea about reason(s)? safe_mode is disabled in PHP.INI for your information.

Thanks for your help in advance!

1 Answer 1

1

How are you calling this function? If you're leaving the $ConnectTimeOutSeconds option at it's default of 1 your CURLOPT_TIMEOUT setting might be causing the issue as it probably takes more than 1 second to connect and return the page.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, $ConnectTimeOutSeconds is set to 10 or 15 seconds, even if it's set to 1 second, it should be returning "false" instead of causing "server dropped connection unexpectedly" error? Am I wrong?
I get "false" and "Operation timed out" with it set at 1 second. Do you get the same "dropped connection" when $URL is google.com or somesuch?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.