简体   繁体   中英

How to send post data using CURL to mailchimp in laravel

In my app i want to post the data in my mailchimp account i have created and mailchimp id and get API KEY . Now I want to post data on mailchimp using php CURL method when i send the data it shows an error like

error page 在此处输入图片说明

post data parameters

在此处输入图片说明

Note : while fetching data using CURL it is perfectly fine.

How can i solve this issue any body help.

this is my method

public function callAPI($method, $url, $data=null)
{
   $curl = curl_init();
    switch ($method)
    {
        case "POST":
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
            case "PUT":
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);                              
            break;
            default:
            if($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
        }
        $headers = array(  
            "Authorization: Basic b3dhaXNfdGFhcnVmZjo5MzM1M" 
        );
        curl_setopt_array($curl, 
        [
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_HTTPHEADER => $headers, 
            CURLOPT_HEADER => true,
            CURLOPT_URL => $url.'?apikey=this is api key'
        ]);

        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
}

this is store function

public function store(Request $request)
{
   $data_array = '
    {
        "id": "PRS08", 
        "title": "108", 
        "handle": "108", 
        "url": "http://boksha.eshmar.com/product/hoodie-kaftan-1", 
        "description": "", 
        "type": "Abaya",
        "vendor": "TALAR NINA", 
        "image_url": "http://boksha.eshmar.com/uploads/products/140/images/cropped/1512195907.png", 
        "variants": 
        [
        {
            "id": "PROD003A", 
            "title": "variants 1", 
            "url": "", 
            "sku": "", 
            "price": 280, 
            "inventory_quantity": 100, 
            "image_url": "http://boksha.eshmar.com/uploads/products/140/images/cropped/1512195907.png", 
            "backorders": "0", 
            "visibility": "visible", 
            "created_at": "2016-02-08T13:06:44+00:00", 
            "updated_at": "2016-02-08T13:06:44+00:00"
        },
        {
            "id": "PROD003B", 
            "title": "variants 2", 
            "url": "", 
            "sku": "", 
            "price": 280, 
            "inventory_quantity": 99, 
            "image_url": "http://boksha.eshmar.com/uploads/products/140/images/cropped/1512195907.png",
             "backorders": "0", 
             "visibility": "true", 
             "created_at": "2016-02-08T22:14:37+00:00", 
             "updated_at": "2016-02-08T22:22:38+00:00"
            }
        ]
    }';
    $url = "https://us7.api.mailchimp.com/3.0/";
    $data = $this->callAPI('POST', $url, $data_array);
    echo "<pre>";
    print_r($data);
}

I see two things that could be an issue.

First - Add a content type to your headers. It looks like you are trying to post a json string so it makes sense that your header would declare that.

$headers = array(  
 'Authorization: Basic b3dhaXNfdGFhcnVmZjo5MzM1M',
 'Content-Type: application/json' //Addded the content type as json.
 );

Secondly - Your json string you are sending is not valid json. You need to remove the comma off the last element in the string.

$data_array = '
        {
            "id": "PRS08", 
            "title": "Php Curl", 
            "name": "Shahid Hussain", 
            "url": "http://fstnv.com", 
            "description": "This is test data"
        }'; //Removed the comma after last element.  Otherwise the json is invalid.

See if that helps you.

Please check the end point, the endpoint is not correct.

the end point should be as follows according to your postman code

https://us7.api.mailchimp.com/3.0//ecommerce/stores/store_001/products

Update the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM