When I run the following cURL page using FireFox:
$url = 'http://api.labelary.com/v1/graphics';
$ext = 'jpg';
$name = 'zplQRCode';
$fileName = $name.'.'.$ext;
$filePath = '@'.$name.'.'.$ext;
$mimeType = 'image/'.$ext;
$cImg1 = curl_file_create($filePath, $mimeType, 'bob');
$cImg2 = new CurlFile($filePath, $mimeType, $fileName);
//$postData = ['file' => $cImg1]; // bool(false);
//$postData = ['file' => $cImg2]; // bool(false);
$postData = ['file' => $filePath]; // ERROR: unknown image format
$header[] = "Accept:application/json";
$header[] = "Content-Type: multipart/form-data";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Language: en-US,en;q=0.5";
//initialise cURL
$ch = curl_init();
$options = [CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15)',
CURLOPT_HTTPHEADER => $header,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "gzip" ];
@curl_setopt_array($ch, $options);
//execute request
$r = curl_exec($ch);
var_dump($r);
curl_close($r);
In Developer Tools > Under Network: I don't see anything under the XHR tag but I do see a GET method under the All tab. Here's what the headers return:
Response headers (202 B)
Connection
close
Content-Type
text/html; charset=UTF-8
Date
Tue, 31 Jul 2018 21:43:07 GMT
Server
Apache
Transfer-Encoding
chunked
X-Powered-By
PHP/5.6.23 ZendServer/8.5.5
Request headers (462 B)
Accept
text/html,application/xhtml+xm…plication/xml;q=0.9,*/*;q=0.8
Accept-Encoding
gzip, deflate
Accept-Language
en-US,en;q=0.5
Cache-Control
max-age=0
Connection
keep-alive
Cookie
_ga=GA1.2.1222505240.153185123…ID=n8cr1upmf5arl790uh2j3uvl81
Host
php.mywebdomain.com
Upgrade-Insecure-Requests
1
User-Agent
Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/61.0
First, I have set to POST but it always runs as GET and anytime I make any changes nothing works. Content Type is always text/html, Host is always my domain, etc..
However, if I run the HTML form using Ajax I get the following response on the JPG file I am uploading.
Access-Control-Allow-Origin
*
Access-Control-Expose-Headers
X-Total-Count
Connection
keep-alive
Content-Encoding
gzip
Content-Length
381
Content-Type
application/json
Date
Tue, 31 Jul 2018 21:47:46 GMT
Vary
Accept-Encoding
X-Vcap-Request-Id
9a96e1d3-047a-442d-707b-e799e2783b7d
Request headers (473 B)
Accept
application/json
Accept-Encoding
gzip, deflate
Accept-Language
en-US,en;q=0.5
Connection
keep-alive
Content-Length
14919
Content-Type
multipart/form-data; boundary=…----------------8913314312416
Host
api.labelary.com
Origin
http://php.mydomain.com
Referer
http://php.mydomain.com/INT/BAR_CODE/Zebra/img.php
User-Agent
Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/61.0
I am not sure what I am doing wrong? All I can think of is the image is being passed from a form vs adding it w/o a HTML form when doing the cURL request.