1. 程式人生 > >curl的$post傳遞多維數組

curl的$post傳遞多維數組

進行 function fields time out top str url地址 模擬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; }

curl的$post傳遞多維數組