1

Have been playing this for a while its a function to connect to an api the un / pw and url are definitely correct.

However it keeps failing with a 413 error code which I understand to mean the data stream to the server was too large but all I am trying to do is connect not send anything yet.

Any suggestions? Thanks

function call_api () {

    $username = "***api un here***";
    $password = "***api pw here***";

    $url = "http://api.domain.com";

    $api_connect = curl_init(); 
    curl_setopt($api_connect, CURLOPT_URL, $url);   
    curl_setopt($api_connect, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($api_connect, CURLOPT_CONNECTTIMEOUT, 30); 
    curl_setopt($api_connect, CURLOPT_TIMEOUT, 30); 
    curl_setopt($api_connect, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($api_connect, CURLOPT_POST, true); 
    curl_setopt($api_connect, CURLOPT_HEADER, 1); 

    $result = curl_exec($api_connect);
    $err = curl_error($api_connect);  

    if( curl_errno($api_connect) )
    {   
        $result = 'ERROR -> ' . curl_errno($api_connect) . ': ' . curl_error($api_connect);
    }
    else 
    {   
        $returnCode = (int)curl_getinfo($api_connect, CURLINFO_HTTP_CODE);              
        switch($returnCode)
        {      
            case 200: 
                 $result = 'HAVE CONNECTION -> ' . $returnCode; 
                 break;    
            default:     
                 $result = 'HTTP ERROR -> ' . $returnCode;        
                 break; 
        }
    }  

    curl_close($api_connect);
    echo $result;

}

UPDATE

To add to this

curl_errno($api_connect) returns 0

curl_error($api_connect) returns empty string

and print_r(curl_getinfo($api_connect) returns the below

Array
(
    [url] => https://api.domain.com
    [content_type] => text/html; charset=iso-8859-1
    [http_code] => 413
    [header_size] => 197
    [request_size] => 218
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.065667
    [namelookup_time] => 0.001792
    [connect_time] => 0.010429
    [pretransfer_time] => 0.056323
    [size_upload] => 0
    [size_download] => 408
    [speed_download] => 6213
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => -1
    [starttransfer_time] => 0.065638
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

    [redirect_url] => 
)
megaSteve4
  • 1,760
  • 1
  • 17
  • 24
  • 2
    http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error -- `413 Request Entity Too Large` – Orbling Oct 16 '13 at 16:09
  • 4
    Is the server using that code to mean that, or is it sending another message with it? – Orbling Oct 16 '13 at 16:11
  • Stop overwriting `$result` with your gibberish error message, it may have a *descriptive* error message to go along with the 413. – Sammitch Oct 16 '13 at 16:19
  • 1
    Thanks @Orbling I have now updated my question with more debug info. Unfortunately there doesn't seem to be much more useful info – megaSteve4 Oct 16 '13 at 16:59
  • Try dumping the response headers, rather than having curl process them. – Orbling Oct 16 '13 at 17:10
  • Try the same parameters from curl on command line with `-v` – Anthony Oct 16 '13 at 17:35
  • The server may be set to return `413` when the request is too small (or empty) as it appears to be with your example. Try changing the request type to `HEAD`. – Anthony Oct 16 '13 at 17:39

1 Answers1

1

Try setting request content explicitly to zero:

curl_setopt($api_connect, CURLOPT_POSTFIELDS, ''); 

It helped me when I had same trouble.

Gleb Varenov
  • 2,795
  • 3
  • 18
  • 18