Upload item image using Square Connect API and PHP -
i've reviewed old questions posted here on stackoverflow issue. didn't find example php integration.
here sample of code it's failing
$url = 'https://connect.squareup.com/v1/me/items/9999999/image'; $auth_bearer = 'authorization: bearer ' . $this->accesstoken; $image_data = base64_encode(file_get_contents('image.jpeg')); $header = array( $auth_bearer, 'accept: application/json', 'content-type: multipart/form-data; boundary=boundary', ); $ch = curl_init($url); curl_setopt($ch, curlopt_post, true); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_httpheader, $header); curl_setopt($ch, curlopt_postfields, 'files=' . $image_data); $head = curl_exec($ch); curl_close($ch); $response = json_decode($head); echo "<pre>"; print_r($response); echo "</pre>";
and nothing happens... here?
thanks
you need post raw image data (not base64 encoded) proper multipart header file object. here's working example (replace access_token
, item_id
, , image_file
).
<?php function uploaditemimage($url, $access_token, $image_file) { $headers = ["authorization: bearer $access_token"]; $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_postfields, ['image_data' => "@$image_file"]); curl_setopt($ch, curlopt_httpheader, $headers); curl_setopt($ch, curlopt_returntransfer, 1); $data = curl_exec($ch); $return_status = curl_getinfo($ch, curlinfo_http_code); print "post $url status $return_status\n"; curl_close($ch); return $data ? json_decode($data) : false; } print_r( uploaditemimage( 'https://connect.squareup.com/v1/me/items/item_id/image', 'access_token', 'image_file.jpg' ) ); ?>
Comments
Post a Comment