簡體   English   中英

使用curl對某個api執行POST時出錯。 它的錯誤是什么?

[英]Error while using curl for doing a POST to a certain api. What is the bug in it?

<?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

代碼中有錯誤嗎? 以及如何調試呢?

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM