Tuesday, October 31, 2023

PHP notify imageFile (from image in server)

 <?php

/*-------------line noti----------------------*/

$line_api = 'https://notify-api.line.me/api/notify';

$access_token = 'your token';


$message = 'test'; //text max 1,000 charecter

$filepath='2.jpg';

$filename = realpath($filepath);

$finfo = new \finfo(FILEINFO_MIME_TYPE);

$mimetype = $finfo->file($filename);


if (function_exists('curl_file_create')) {

$cFile = curl_file_create($filename, $mimetype, basename($filename));

} else {

$cFile = '@'.realpath($imageFile );

}


$message_data = array(

'message' => $message,

'imageFile' => $cFile

);


$result = send_notify_message($line_api, $access_token, $message_data);


echo '<pre>';

print_r($result);

function send_notify_message($line_api, $access_token, $message_data){

$headers = array('Method: POST', 'Content-type: multipart/form-data', 'Authorization: Bearer '.$access_token );


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $line_api);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

curl_setopt($ch, CURLOPT_POSTFIELDS, $message_data);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);

// Check Error

if(curl_error($ch))

{

$return_array = array( 'status' => '000: send fail', 'message' => curl_error($ch) );

}

else

{

$return_array = json_decode($result, true);

}

curl_close($ch);

return $return_array;

}

echo '</pre>';


/*-------------line noti----------------------*/

?>