问题
Trying to get square connect API image upload working using PHP.
I used the square connect API guide: docs.connect.squareup.com/api/connect/v1/#post-image
Tried two different ways based on what I found on StackOverflow and Google searching.
Method 1) regular curl request: https://gist.github.com/delalis/17c3c111e3b42df127ed
Method 2) using CURLFile (php >=5.5 only) https://gist.github.com/delalis/5c7ecc2aaa024927b360
Both methods gave me this empty reply from server error:
Error: "Empty reply from server" - Code: 52
I am able to connect to square to do other functions no problem, image uploading however is proving to be quite difficult!
Any help would be greatly appreciated.
回答1:
Here's a working PHP example that I usually share with people: https://gist.github.com/tdeck/7118c7128a4a2b653d11
<?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;type=image/jpeg"]);
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 to $url with 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'
)
);
?>
来源:https://stackoverflow.com/questions/31349302/square-connect-api-image-upload-empty-reply-from-server-code-52-error