-1

I try to send a request to an API (mailchimp) using cURL. It works with GET, however, I have problems converting the GET-URl into a proper associative PHP array to send via POST. EDIT Because as MarcB commented, GET requests have some negative sides, like a lenght limit, etc.

Please note that I replaced the values of the variables with asteriks (*).
This is the working GET-parameter:

https://us3.api.mailchimp.com/2.0/ecomm/order-add.json?apikey=***
&order[id]=***
&order[campaign_id]=***
&order[email_id]=***
&order[total]=***
&order[store_id]=***
&order[items][0][product_id]=***
&order[items][0][category_id]=***
&order[items][0][category_name]=***
&order[items][1][product_id]=***
&order[items][1][category_id]=***
&order[items][1][category_name]=***
...


This is the PHP array for the POST request:

Array
(
    [apikey] => ***
    [order] => Array
        (
            [id] => ***
            [campaign_id] => ***
            [email_id] => ***
            [total] => ***
            [order_date] => ***
            [shipping] => ***
            [tax] => ***
            [store_id] => ***
            [store_name] => ***
            [items] => Array
                (
                    [0] => Array
                        (
                            [product_id] => ***
                            [category_id] => ***
                            [category_name] => ***
                        )

                    [1] => Array
                        (
                            [product_id] => ***
                            [category_id] => ***
                            [category_name] => ***
                        )

                    ...

                )

        )

)


Response is:

Validation error: {\"order\":\"Please enter a struct\\\/associative array\"}


Any idea, what I am missing here? Isn't the structure here the same?

--
UPDATE:
This is the curl code:

function httpPost($url,$params)
{
  $postData = '';
   //create name value pairs seperated by &
   foreach($params as $k => $v) 
   { 
      $postData .= $k . '='.$v.'&'; 
   }
   rtrim($postData, '&');

    $ch = curl_init();  

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_POST, count($postData));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);    

    $output=curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);
    return array($output, $httpCode);

}

print_r( httpPost( $mc_api_url, $curl_data ) );
6
  • 2
    That's a pretty dang long query string. That much data should go via post, not a GET query. query strings are length limited for a reason, and yours might be getting truncated somewhere.
    – Marc B
    May 23, 2014 at 19:42
  • Hi @MarcB, yes thank you, you are right about that. That's why I want to do it via POST and not GET. The GET was just trying out if it works, but now I get this error when using POST and cannot see why?
    – Chris
    May 23, 2014 at 19:43
  • well, that validation error looks like JSON. does the mailchimp API expect json? are you doing a proper json_encode() if that's the case?
    – Marc B
    May 23, 2014 at 19:45
  • No, it's not only with json, unfortunately. :(
    – Chris
    May 23, 2014 at 19:49
  • can you show you code that is generating the cURL POST request? If you have already generated a query string for GET there is no reason whatsoever you shouldn't be able to simple use that same query string for POST. The only question is does the target server expect different request formats for GET vs. POST
    – Mike Brant
    May 23, 2014 at 19:58

2 Answers 2

1

OK for starters, you should not be manually building a query string. You should either simply pass your associative array to CURLOPT_POSTFIELDS or build the query string using something like http_build_query() (http://www.php.net/manual/en/function.http-build-query.php).

Second, you should use a value of true for your CURLOPT_POST field.

Of course this is all assuming that API is expecting an application/x-www-form-urlencoded POST request. My guess is that this may not be the case, as your response seem like it is in JSON.

A brief glance of the MailChimp API documentation at http://apidocs.mailchimp.com/api/2.0/

Notes this:

Request data is passed to the API by POSTing JSON objects to the API endpoints with the appropriate parameters.

If you are actually using this API version, then my guess is you need to do be forming JSON objects in the expected format nd using application/json as ContentType header.

If it helps, I have a very lightweight cURL-based REST client that you are welcome to use to more easily interact with RESTful services. Check it out here: https://github.com/mikecbrant/php-rest-client

0

I had the same error, and got it to work by adding a few extra conditions to my curl call. Try adding these and see if it works:

$data = json_encode($data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.