Search code examples
phpcurlsquare-connect

Square-Connect Inventory Update cURL Call


I am trying to update my square inventory from my inventory database website and I keep getting this error.

Response:{"type":"bad_request","message":"Missing required parameter `quantity_delta`"}

I am adding the quantity_delta field and adjustment_type to the cURL call because that is what the documentation says, there are 3 options in the documentation and only 1 of them has (optional) next to it so I am using the 2 that appear to be required. I can't capture the POST body to see exactly how the call is going out, maybe a type or json_encode issue, so debugging this is giving me an issue.

I am also writing the headers and the response to a text file fore easy reading.

Here is the code:

$i = $_GET['id'];
$n = $_GET['name'];
$q = $_GET['qty'];
$s = $_GET['sku'];
$c = $_GET['current'];
$sync = $_GET['sync'];

if($c > $q){
    $up = $q - $c;
    $reason = "SALE";
}else{
    $up = $c + $q;
    $reason = "RECEIVE_STOCK";
}
$postData = array(
    "quantity_delta" => $up,
    "adjustment_type" => $reason);
$b = json_encode($postData);
$fp = fopen('curlOut.txt', 'rw+');
fopen('curlOut.txt', 'rw+');
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Bearer *****_******' ));
curl_setopt($curl, CURLOPT_URL, "https://connect.squareup.com/v1/me/inventory/".$i."");
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $b);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_STDERR, $fp);


if(!curl_exec($curl)){
    die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
$filename = 'curlOut.txt';
if (is_writable($filename)){
    echo 'The file is writeable';
}else{
    echo 'nope';
}
$ch = curl_exec ($curl);
$sentCall = curl_getinfo($curl, CURLINFO_HEADER_OUT);
$dump = fopen("curlOut.txt","a") or die("Unable to open file!");
$dumptxt = "Header Info:".$sentCall . "Response:".$ch."\n\n";
fwrite($dump,$dumptxt);
curl_close ($curl);
fclose('curlOut.txt');
var_dump(json_decode($ch,true));

Can you please tell me what I am doing wrong? I have been trying for days to figure out what is wrong with my cURL call. I can do cURL calls to read data from the square-connect API with no issues. I also have some repetitive code in here to display output/response in different ways hoping for more information. I will also post the header info that I get using CULINFO_HEADER_OUT.

Header Info:POST /v1/me/inventory/011a799a-****-****-****-4f5b70dc1494 HTTP/1.1
Host: connect.squareup.com
Accept: */*
Authorization: Bearer *****_*****
Content-Length: 47
Content-Type: application/x-www-form-urlencoded

Thank You.


Solution

  • I believe this error is occurring because your request's Content-Type header is currently application/x-www-form-urlencoded. Requests to the Connect API must have a Content-Type of application/json to match your request body.

    This was clearly an unhelpful error message to receive in this case; I will work with the API engineering team to improve it.