handle fopen image_path contents fread handle filesize image_path fclo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
$handle = fopen($image_path, "r");
$contents = fread($handle, filesize($image_path));
fclose($handle);
$eol = "\r\n"; //default line-break for mime type
$BOUNDARY = '------='.md5(time()); //random boundaryid, is a separator for each param on my post curl function
$BODY = ""; //init my curl body
$BODY.= $BOUNDARY . $eol; //start param header
$BODY.= 'Content-Type: ' . $image_type . ';' . ' name=' . $image_name . $eol; //Same before row
$BODY.= 'Content-Transfer-Encoding: binary' . $eol;
$BODY .= 'Content-Disposition: form-data; name="file-data" filename="' . $image_name . '"' . $eol . $eol;
$BODY.= $contents; //
$headers = array(
'Accept-Encoding: gzip,deflate ' . $eol
. $this->getAuthHeaderStr() . $eol
. 'Accept: application/json' . $eol
. 'Content-Type: multipart/form-data; boundary="' . $BOUNDARY . '"' . $eol
. 'Content-Transfer-Encoding: binary' . $eol
. 'MIME-Version: 1.0' . $eol
//. 'Content-Length: ' . filesize($image_path) . $eol
// . 'Host: localhost:8080' . $eol
. 'Connection: Keep-Alive' . $eol
. 'User-Agent: ' . $_SERVER['HTTP_USER_AGENT'] . $eol
// . 'Expect'
);
$ch = curl_init(); //init curl
if ($headers !== array()) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
// curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $path); //setting our api post url
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // call return content
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // navigate the endpoint
curl_setopt($ch, CURLOPT_POST, true); //set as post
curl_setopt($ch, CURLOPT_POSTFIELDS, array('data'=>$BODY)); // set our $BODY
$response = curl_exec($ch);