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

Thursday, October 26, 2023

vba to html post

 Sub test3()

    Set ws = Worksheets("ÃÒ¡ÒÃÊÑ觫×éÍ")

    R = 5

    ws.Cells(1, 3) = ""

    vtext = ""

    i = 1


    While ws.Cells(R, 1) <> ""

        vtext = vtext & i & "" & ws.Cells(R, 3) & "  " & Int(ws.Cells(R, 13) * 100)

        R = R + 1

        i = i + 1

    Wend


    ' สร้าง HTTP Request Object

    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")


    ' สร้าง URL

    sURL = "http://192.168.0.105/xampp/line/inj.php?text=" & vtext


    ' ส่งคำขอ HTTP GET

    objHTTP.Open "GET", sURL, False

    objHTTP.setRequestHeader "Content-Type", "text/xml"

    objHTTP.send ""


    ' รอให้การเรียก URL เสร็จสิ้นก่อนที่จะดำเนินการถัดไป

    Do While objHTTP.readyState <> 4

        DoEvents

    Loop


    ' ตรวจสอบการเรียก URL ว่าเสร็จสิ้นหรือไม่ (สถานะ 200 หมายถึง OK)

    If objHTTP.Status = 200 Then

        ' การเรียก URL เสร็จสิ้น และส่งข้อมูลเพียงครั้งเดียว

        MsgBox "ส่งข้อมูลสำเร็จ"

    Else

        ' การเรียก URL มีปัญหา

        MsgBox "มีปัญหาในการส่งข้อมูล"

    End If


    ' ลบอ็อบเจ็กต์ HTTP Request

    Set objHTTP = Nothing

End Sub


VBA image to base64

 Public Function EncodeFile(strPicPath As String) As String

    Const adTypeBinary = 1          ' Binary file is encoded

    ' Variables for encoding
    Dim objXML
    Dim objDocElem

    ' Variable for reading binary picture
    Dim objStream

    ' Open data stream from picture
    Set objStream = CreateObject("ADODB.Stream")
    objStream.Type = adTypeBinary
    objStream.Open
    objStream.LoadFromFile (strPicPath)

    ' Create XML Document object and root node
    ' that will contain the data
    Set objXML = CreateObject("MSXml2.DOMDocument")
    Set objDocElem = objXML.createElement("Base64Data")
    objDocElem.dataType = "bin.base64"

    ' Set binary value
    objDocElem.nodeTypedValue = objStream.Read()

    ' Get base64 value
    EncodeFile = objDocElem.Text

    ' Clean all
    Set objXML = Nothing
    Set objDocElem = Nothing
    Set objStream = Nothing

End Function

REF-->https://stackoverflow.com/questions/2043393/convert-image-jpg-to-base64-in-excel-vba