1

已经玩了一段时间了,它是连接到 api 的功能,un / pw 和 url 绝对正确。

但是它一直失败并出现 413 错误代码,我理解这意味着到服务器的数据流太大,但我要做的只是连接而不发送任何东西。

有什么建议么?谢谢

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;

}

更新

添加到此

curl_errno($api_connect) 返回 0

curl_error($api_connect) 返回空字符串

print_r(curl_getinfo($api_connect)返回以下

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

1 回答 1

1

尝试将请求内容显式设置为零:

curl_setopt($api_connect, CURLOPT_POSTFIELDS, ''); 

当我遇到同样的麻烦时,它帮助了我。

于 2014-04-23T09:51:06.043 回答