העלאת קובץ בינארי בCURL עם PHP
-
שלום
אני משתמש בCURL כדי להעלות קבצים דרך PHP.
הצורה הרגילה היא להשתמש בCURLFile כדי לכלול את הקובץ, אבל יש לי קובץ בינארי שאני רוצה להעלות, ולא מוצא איך לעשות את זה (לא רוצה לשמור אותו ואז להעלות, אני מאמין שיש דרך יותר קצרה)
אשמח לקבל עזרה
תודה רבה<?php $file = 'files/song.mp3'; // ככה אני תמיד עושה $upload = new CURLFile(realpath($file)); // אבל אני רוצה לעשות ככה // $upload = file_get_contents($file); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, compact('upload')); $value = curl_exec($ch); curl_close ($ch);פורסם במקור בפורום CODE613 ב07/01/2018 23:23 (+02:00)
-
למה אתה משתמש בCURL?
אתה יכול להשתמש רק ב file_get_contents.
הפונקציה הזאת לא נועדה רק לקבל קבצים מקומים, אלא גם לתקשורת (אותה פעולה שעושה הCURL).
כך:$file = 'files/song.mp3'; $body = file_get_contents($file); $opts = array('http' => array( 'method' => 'POST', 'content' => $body, 'follow_location' => false) ); $context = stream_context_create($opts); $url = 'https://www.a.co.il/'; $result = file_get_contents($url, FALSE, $context);(רק צריך להתאים את צורת השליחה.
אתה תצטרך להשתמש במחלקה שתסדר את הבודי.
אני משתמש במחלקה הזאת:class BodyPost { // part "multipart/form-data" public static function PartPost($name, $val) { $body = 'Content-Disposition: form-data; name="' . $name . '"'; // check instance of oFile if($val instanceof oFile) { $file = $val->Name(); $mime = $val->Mime(); $cont = $val->Content(); $body .= '; filename="' . $file . '"' . "\r\n"; $body .= 'Content-Type: ' . $mime ."\r\n\r\n"; $body .= $cont."\r\n"; } else $body .= "\r\n\r\n".$val."\r\n"; return $body; } public static function Get(array $post, $delimiter = '-------------0123456789') { if(is_array($post) && !empty($post)) { $bool = true; //foreach($post as $val) if($val instanceof oFile) {$bool = true; break; }; if($bool) { $ret = ''; foreach($post as $name=>$val) $ret .= '--' . $delimiter. "\r\n". self::PartPost($name, $val); $ret .= "--" . $delimiter . "--\r\n"; } else $ret = http_build_query($post); } else throw new \Exception('Error input param!'); return $ret; } } class oFile { private $name; private $mime; private $content; public function __construct($name, $mime=null, $content=null) { if(is_null($content)) { $info = pathinfo($name); // check is exist and readable file if(!empty($info['basename']) && is_readable($name)) { $this->name = $info['basename']; // get MIME $this->mime = mime_content_type($name); // load file $content = file_get_contents($name); if($content!==false) { $this->content = $content; } else { throw new Exception('Don`t get content - "'.$name.'"'); } } else { throw new Exception('Error param'); } } else { $this->name = $name; if(is_null($mime)) $mime = mime_content_type($name); $this->mime = $mime; $this->content = $content; }; } public function Name() { return $this->name; } public function Mime() { return $this->mime; } public function Content() { return $this->content; } }ואז כך מתבצעת ההעלאה:)
$file = 'files/song.mp3'; $file = new oFile($file); $delimiter = '----'.uniqid(); $body = BodyPost::Get($file, $delimiter); $opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-Type: multipart/form-data; boundary='.$delimiter, 'content' => $body, 'follow_location' => false) ); $context = stream_context_create($opts); $url = 'https://www.a.co.il/'; $result = file_get_contents($url, FALSE, $context);פורסם במקור בפורום CODE613 ב08/01/2018 14:05 (+02:00)
-
אתה צודק, שכחתי משהו, שהפרמטר שמעבירים ל BodyPost::Get, צריך להיות מערך.
כזה:<?php $file = 'AA.txt'; $file = new oFile($file); // מערך: $body = array($file); $delimiter = '----'.uniqid(); $body = BodyPost::Get($body, $delimiter); $opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-Type: multipart/form-data; boundary='.$delimiter, 'content' => $body, 'follow_location' => false) ); $context = stream_context_create($opts); $url = 'https://www.a.co.il/'; $result = file_get_contents($url, FALSE, $context); ?>פורסם במקור בפורום CODE613 ב11/01/2018 14:36 (+02:00)
שלום! נראה שהשיחה הזו מעניינת אותך, אבל עדיין אין לך חשבון.
נמאס לכם לגלול בין אותם הפוסטים בכל ביקור? כשנרשמים לחשבון, תמיד תחזרו בדיוק למקום שבו הייתם קודם, ותוכלו לבחור לקבל התראות על תגובות חדשות (בין אם במייל, ובין אם בהתראת פוש). תוכלו גם לשמור סימניות ולפרגן ב-upvote לפוסטים כדי להביע הערכה לחברי קהילה אחרים.
בעזרת התרומה שלך, הפוסט הזה יכול להיות אפילו טוב יותר 💗
הרשמה התחברות