curl的$post傳遞多維數組


php curl傳數組的話只能傳一維數組,如果想傳多維數組:兩個方法:

1.轉換成json在傳輸

2.

//通過curl模擬post的請求;
function SendDataByCurl($url,$data=array()){
    //對空格進行轉義
    $url = str_replace(' ','+',$url);
    $ch = curl_init();
    //設置選項,包括URL
    curl_setopt($ch, CURLOPT_URL, "$url");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch,CURLOPT_TIMEOUT,3);  //定義超時3秒鍾  
     // POST數據
    curl_setopt($ch, CURLOPT_POST, 1);
    // 把post的變量加上
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));    //所需傳的數組用http_bulid_query()函數處理一下,就ok了
    
    //執行並獲取url地址的內容
    $output = curl_exec($ch);
    $errorCode = curl_errno($ch);
    //釋放curl句柄
    curl_close($ch);
    if(0 !== $errorCode) {
        return false;
    }
    return $output;

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM