使用curl对某个api执行POST时出错。它的错误是什么?

时间:2010-12-13 05:45:52

标签: php curl

<?php 


function curl_post($url, array $post = NULL, array $options = array()) 
{ 
    $defaults = array( 
        CURLOPT_POST => 1, 
        CURLOPT_HEADER => 0, 
        CURLOPT_URL => $url, 
        CURLOPT_FRESH_CONNECT => 1, 
        CURLOPT_RETURNTRANSFER => 1, 
        CURLOPT_FORBID_REUSE => 1, 
        CURLOPT_TIMEOUT => 4, 
        CURLOPT_POSTFIELDS => http_build_query($post) 
    ); 

    $ch = curl_init(); 
    curl_setopt_array($ch, ($options + $defaults)); 
    if( ! $result = curl_exec($ch)) 
    { 
        trigger_error(curl_error($ch)); 
    } 
    curl_close($ch); 
    return $result; 
} 

curl_post('http://pastebin.com/api_public.php');
?>

我得到的错误就是这个

Warning: http_build_query() [function.http-build-query]: Parameter 1 expected to be Array or Object. Incorrect value given in /opt/lampp/htdocs/pastebinapp/pasteb.php on line 20

Notice: name lookup timed out in /opt/lampp/htdocs/pastebinapp/pasteb.php on line 27

代码中是否有错误?以及如何调试它?

1 个答案:

答案 0 :(得分:1)

($options + $defaults) //doesn't preserve array keys

curl_post($url, array $post = null, array $options = array()) //$post needs to be array not null