//【定制化图像开放平台】PHP示例代码
$token = '';
$url = 'https://aip.baidubce.com/XXXXXXXX?access_token=' . $token; //XXXXXXXX替换申请后的接口地址
$img = file_get_contents("./img2/1.jpg");//图片路径
$img = base64_encode($img); //图片转码
$bodys = array(
'image' => $img
);
$bodys = json_encode($bodys);//参数转码
$res = request_post($url, $bodys);
echo $res;
function request_post($url = '', $param = '')
{
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
// 初始化curl
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $postUrl);
curl_setopt($curl, CURLOPT_HEADER, 0);
// 要求结果为字符串且输出到屏幕上
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// post提交方式
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
// 运行curl
$data = curl_exec($curl);
curl_close($curl);
return $data;
}