简体   繁体   中英

how to get data from mailchimp using php curl in laravel

I my Laravel app i am using php curl for mailchimp to store and get products data is storing but when i want to fetch data using foreach loop it is showing an error Trying to get property 'stores' of non-object how can i solve this thanks in advance

this is the result when i use dd() 在此处输入图片说明

this is the request

public function get_store()
    {
        $id = "store_1159";
        $url = "https://us7.api.mailchimp.com/3.0/ecommerce/stores";
        $data = callAPI('GET', $url, false);
        $stores = json_decode($data);
        //dd($stores );
        $result = array();
        foreach ($stores as $store) {
            echo $store->id;            
        }
    }


this is php curl main function

  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 "PATCH":
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);                              
            break;
            case "DELETE":
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); 
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            default:
            if($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
       }

        $headers = array(  
            "--user: credentials",
            "Authorization: Basic 1232132131",
            "Content-Type: application/json",
            "Postman-Token: 163f7134-68ca-45bb-9420-ebf2bef7f447",
            "cache-control: no-cache"
         );
        curl_setopt_array($curl, 
        [
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_HTTPHEADER => $headers, 
            CURLOPT_HEADER => false,
            CURLOPT_URL => $url.'?apikey=12313123'
        ]);

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

try this,

$data = callAPI('GET', $url, false);
$data = json_decode($data);

foreach ($data->stores as $store)
{
    echo $store->id;            
}

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