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----------------------*/

?>

VBA FTP

  Declare PtrSafe Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" ( _

        ByVal hInternetSession As Long, ByVal sServerName As String, _

        ByVal nServerPort As Integer, ByVal sUserName As String, _

        ByVal sPassword As String, ByVal lService As Long, _

        ByVal lFlags As Long, ByVal lContext As Long) As Long

    Declare PtrSafe Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" ( _

        ByVal sAgent As String, ByVal lAccessType As Long, _

        ByVal sProxyName As String, _

        ByVal sProxyBypass As String, ByVal lFlags As Long) As Long

    Declare PtrSafe Function FtpSetCurrentDirectory Lib "wininet.dll" Alias _

     "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, _

        ByVal lpszDirectory As String) As Boolean

    Declare PtrSafe Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" ( _

        ByVal hConnect As Long, _

        ByVal lpszLocalFile As String, _

        ByVal lpszNewRemoteFile As String, _

        ByVal dwFlags As Long, _

        ByRef dwContext As Long) As Boolean


Sub simpleFtpFileUpload()


    Dim ftp, FTP_PORT, user, password, loc_file, remote_file, ftp_folder As Variant

    'ftp_folder = "/EXPORT"

    'loc_file = ThisWorkbook.Path & "\readme.txt"

    'remote_file = ftp_folder & "/readme.txt"

    loc_file = "d:\2.jpg"

    remote_file = "2.jpg"

    FTP_PORT = "21"

    user = "ajay"

    password = "ajay"

    ftp = "192.168.0.86"


    Internet_OK = InternetOpen("", 1, "", "", 0)

    If Internet_OK Then

        FTP_OK = InternetConnect(Internet_OK, ftp, FTP_PORT, user, password, 1, 0, 0) ' INTERNET_DEFAULT_FTP_PORT or port no

        If FtpSetCurrentDirectory(FTP_OK, "/") Then

            Success = FtpPutFile(FTP_OK, loc_file, remote_file, FTP_TRANSFER_TYPE_BINARY, 0)

        End If

    End If

    If Success Then

        Debug.Print "ftp success ;)"

        MsgBox "ftp success ;)"

    Else

        Debug.Print "ftp failure :("

        MsgBox "ftp failure :("

    End If

End Sub



//////////////////////////////////////

CR: https://stackoverflow.com/questions/7737691/upload-file-via-ftp-from-excel-vba