【问题标题】:How to send post data using CURL to mailchimp in laravel如何使用 CURL 将帖子数据发送到 laravel 中的 mailchimp
【发布时间】:2019-01-19 07:11:16
【问题描述】:

在我的应用程序中,我想将数据发布到我创建的 ma​​ilchimp 帐户和 ma​​ilchimp id 并获取 API KEY。现在我想使用 php CURL 方法在 ma​​ilchimp 上发布数据,当我发送数据时会显示类似

的错误

错误页面

发布数据参数

注意:在使用CURL 获取数据时,它完全没问题。

任何机构的帮助,我该如何解决这个问题。

这是我的方法

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;
}

这是存储功能

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);
}

【问题讨论】:

  • 你可以试试 curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); laravelcode.com/post/…
  • 我做到了,但得到了同样的错误
  • 帖子期望的参数是什么?
  • 试试这个 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, ");
  • 参数见上图,我使用了 CURLOPT_CUSTOMREQUEST 但不起作用

标签: php laravel curl


【解决方案1】:

我发现有两件事可能是个问题。

首先 - 将内容类型添加到您的标题中。看起来您正在尝试发布一个 json 字符串,因此您的标头会声明它是有道理的。

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

其次 - 您发送的 json 字符串不是有效的 json。您需要删除字符串中最后一个元素的逗号。

$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.

看看对你有没有帮助。

【讨论】:

  • 这不起作用请查看我已更新正确参数的$data_array
  • 帖子期望的参数是什么?
  • 实际上,您的代码中没有提到参数。 API 期望类似:example.com?id=123&name='Ted'。 id 和 name 是参数。您的 API 对您有什么期望?
  • $data_array 变量 id , title 和 handle 等中给出的所有参数。
【解决方案2】:

请检查终点, 端点不正确。

根据您的邮递员代码,终点应如下所示

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

更新相同。

【讨论】:

    猜你喜欢
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-07
    相关资源
    最近更新 更多