php curl post file

最近碰到一個需求:需要在PHP中向一個接口POST發送一個文件,最終發現用curl可以實現。

代碼示例

//php code
$arr = array(
    'file' => '@' . $filePath,
    'isZip' => 1,
);
$url = 'http://test.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
$res = curl_exec($ch);

引用stackoverflow

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章