Curl 傳遞 POST 資料,並取得回傳值
/** * VECTOR COOL * https://vector.cool */ //用curl傳post並取回傳值 //一定要傳絕對路徑 function curl_post($url,$post) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST,true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST'); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); $result = curl_exec($ch); curl_close ($ch); return $result; }
使用範例:
/** * VECTOR COOL * https://vector.cool */ $url='http://tw.yahoo.com'; $post_value= array( 'name' => 'JACK', 'age' => '20', 'phone' => '0968123456', 'address' => '台灣' ); echo curl_post($url,$post_value);
[……]