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] =>
)