How do I create a [] array for entitled_collection_ids in php?

B_J__Nolletti
New Member
4 0 0

I'm having trouble understanding how to make an array for the value of entitled_collection_ids.  I'm using php and encoding the query string with http_build_query(). 

I've tried it a number of ways, but always get the error:

[response] => {"errors":{"entitled_collection_ids":"expected Hash to be a Array"}} )

 

PHP Code:

$MODIFY_DATA = array (
      "price_rule" => array (
        "title" => "Ambassador Credit",
        "target_type" => "line_item",
        "target_selection" => "entitled",
        "entitled_collection_ids" => array(array(2723640119)),
        "allocation_method" => "across",
        "value_type" => "fixed_amount",
        "value" => $amount * -1,
        "customer_selection" => "all",
        "starts_at" => "2017-01-1T17:59:10Z",
            "usage_limit" => "1"
        )
    );
    $response = shopify_call($TOKEN, $STORE_URL, "/admin/price_rules.json", $MODIFY_DATA, 'POST');

 

function shopify_call($token, $shop, $api_endpoint, $query = array(), $method = 'GET', $request_headers = array()) {

    // Build URL
    $url = "https://" . $shop . ".myshopify.com" . $api_endpoint;
    if (!is_null($query) && in_array($method, array('GET',     'DELETE'))) $url = $url . "?" . http_build_query($query);
    // Configure cURL
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, TRUE);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    // curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 3);
    // curl_setopt($curl, CURLOPT_SSLVERSION, 3);
    curl_setopt($curl, CURLOPT_USERAGENT, 'My New Shopify App v.1');
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    // Setup headers
    $request_headers[] = "";
    if (!is_null($token)) $request_headers[] = "X-Shopify-Access-Token: " . $token;
    curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);
    if ($method != 'GET' && in_array($method, array('POST', 'PUT'))) {
        if (is_array($query)) $query = http_build_query($query);
        curl_setopt ($curl, CURLOPT_POSTFIELDS, $query);
    }

    // Send request to Shopify and capture any errors
    $response = curl_exec($curl);
    $error_number = curl_errno($curl);
    $error_message = curl_error($curl);
    // Close cURL to be nice
    curl_close($curl);
    // Return an error is cURL has a problem
    if ($error_number) {
        return $error_message;
    } else {
        // No error, return Shopify's response by parsing out the body and the headers
        $response = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
        // Convert headers into an array
        $headers = array();
        $header_data = explode("\n",$response[0]);
        $headers['status'] = $header_data[0]; // Does not contain a key, have to explicitly set
        array_shift($header_data); // Remove status, we've already set it above
        foreach($header_data as $part) {
            $h = explode(":", $part);
            $headers[trim($h[0])] = trim($h[1]);
        }
        // Return headers and Shopify's response
        return array('headers' => $headers, 'response' => $response[1]);
    }

}

0 Likes
Alex
Shopify Staff
Shopify Staff
1555 81 295

Hey BJ.

Have you had any success changing "entitled_collection_ids" => array(array(2723640119)) to "entitled_collection_ids" => array(2723640119)?

entitled_collection_ids is meant to be a simple array of ID values rather than a simple value array containing key/value JSON objects. That might not be how PHP works but it's just a thought I thought I'd pass along.

Cheers.

0 Likes
B_J__Nolletti
New Member
4 0 0

Thanks Alex.  I have tied that.  I get the same error.

I've tried:
"entitled_collection_ids" => array(2723640119)
"entitled_collection_ids" => array(array(2723640119))
"entitled_collection_ids" => 2723640119
"entitled_collection_ids" => [2723640119]
"entitled_collection_ids" => array("0" => 2723640119)
"entitled_collection_ids" => array(array("0" => 2723640119))

All result in the same error...

 

 

0 Likes
Hiral1
New Member
3 0 0

Hey B.J. Nolletti
Did you found any solution?
I'm facing same issue.

0 Likes
MXS
New Member
10 0 0

I have the same problem.

Has anyone find a solution to this?

0 Likes
B_J__Nolletti
New Member
4 0 0

No solutions yet.  I still haven't figured it out.  I've spent so much time on it, I feel like it must be a bug...  Really wish I could get it to work.

0 Likes
GistApps
New Member
1 0 0

http_build_query is not actually capable of creating non-indexed arrays.

The following will fix that

preg_replace('/\%5B\d+\%5D/', '%5B%5D', http_build_query($urlParams))

 

0 Likes