LINE Notify Send Image

<?php
define('LINE_URL'  , "https://notify-api.line.me/api/notify");
define('LINE_TOKEN', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

function notify($message, $fileName){

    $ext = substr($fileName, strrpos($fileName, '.') + 1);

    // 画像取得
    $fileHandle = fopen($fileName, "rb");
    $fileContents = stream_get_contents($fileHandle);
    fclose($fileHandle);

    $boundary = "---------------------".substr(md5(rand(0,32000)), 0, 10);

    $data = "";
    $data .= "--{$boundary}\r\n";
    $data .= "Content-Disposition: form-data; name=message\r\n\r\n{$message}\r\n";
    $data .= "--{$boundary}\r\n";
    $data .= "Content-Disposition: form-data; name=\"imageFile\"; filename=\"{$fileName}\"\r\n";
    $data .= "Content-Type: image/{$ext}\r\n";
    $data .= "\r\n";
    $data .= $fileContents;
    $data .= "\r\n--{$boundary}";
    // 終端
    $data .= "--{$boundary}--\r\n";
    $options = array(
        'http'=>array(
            'method'=>'POST',
            'header'=>"Authorization: Bearer " . LINE_TOKEN . "\r\n"
                . "Content-Type: multipart/form-data; boundary={$boundary}",
            'content' => $data
        )
    );
    $context = stream_context_create($options);
    file_get_contents(LINE_URL, false, $context );
}


/*******
https://aprico-media.com/posts/1824

php 画像 image line notify

https://blog.ko31.com/demo/linenotify/
******/

?>


<?php
if(isset($_POST['image']) && !empty($_POST['image'])){

	$html = htmlspecialchars(file_get_contents($_POST['image']));
	$html = substr($html, strpos($html, "colorImages"));
	$html = substr($html, 1, strpos($html, "colorToAsin"));

	//loop counter
	$lc = 1;
	
	while(strpos($html, "hiRes") !== false){
		preg_match_all('/(?<=hiRes).*\.jpg/', $html, $match);
		$val = $match[0][0];
		$imag = substr($val, 0, strpos($val, "thumb"));
		$html = substr($val, strpos($val, "hiRes"));
		
		//fetch only need strings
		preg_match_all("/https.*\.jpg/", $imag, $match);
		$val = $match[0][0];
		
		$msg = "";
		if($lc == 1){
			$msg = "画像取得\nURL:" . $_POST['image'] . "\n";
		}
		$msg .= $lc++ . "枚目";
		
		notify($msg, $val);
	}
}
?>








<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>画像取得 - LINE Notify</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<style>
h1 {
    margin-bottom: 20px;
}
#form_notify {
    margin-top: 20px;
}
.row {
    margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="col-md-6 col-md-offset-3">
    <h1>画像URL</h1>
    <form id="form_notify" method="POST">
        <div class="row">
            <div class="form-group" id="name-field">
                <label for="image" class="col-lg-4 control-label">※2020/04/19現在 Amazonのみ対応</label>
                <div class="col-lg-8">
                    <input type="text" class="form-control" id="image" name="image" placeholder="画像URL入力">
                </div>
            </div>
        </div>
        <div class="row">
            <div class="form-group" id="submit-field">
                <div class="col-lg-4"></div>
                <div class="col-lg-8">
                    <button id="submit" type="submit" class="btn btn-success">取得する</button>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="form-group" id="result-field">
                <div class="col-lg-4"></div>
                <div class="col-lg-8">
                    <span id="result"></span>
                </div>
            </div>
        </div>
    </form>
</div>

</body>
</html>

この記事が気に入ったらサポートをしてみませんか?