簡體   English   中英

如何從Laravel 5.6中使用CURL中的post方法調用REST-API?

[英]how to Call REST-API with post method in CURL from laravel 5.6?

我正在使用laravel 5.6。我想用post方法調用REST-API 其中URL參數為JSON格式 我只是在工作的Postman中調用了該API。 但是當我嘗試通過CURL方法調用此Laravel控制器時出現錯誤

Array to string conversion

JSON參數為

{"DebtorID":"00000000080000000002","InitiatedDate":"2018-04- 
 20","OrderNo":"1234","SOReference":"Test order",  
 "Lines":[{"PartNo":"1170","QuantityOrdered":5}, 
          {"CommentLine":"true","CommentText":"This is a comment line"},
          {"InventoryID":"000000000K00000000BV","QuantityOrdered":2,  
           "DiscountedPrice":15.67,
            "CustomFieldValues":[
              {SettingID":"1ae102b94dc54dfc8a45",
                "Contents":"Fragile - do not drop"
                   }
                ]
            }
          ],
           "Payments":[{"PaymentRef":"S454873-J5","AmountPaid":50.00}]
        }
}

我的Laravel代碼是

        $curl = curl_init();
        curl_setopt_array($curl, array(
        CURLOPT_URL =>$request->url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYPEER=>false,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30000,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => $request->data,
        CURLOPT_HTTPHEADER => array(
            // Set here requred headers
            "accept: */*",
            "accept-language: en-US,en;q=0.8",
            "content-type: application/json",
            "Accept: application/json",
        ),
    ));
    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);

這是郵遞員打來的示例圖片。 當我直接調用此API時,它將返回200。 在此處輸入圖片說明

但是當我從laravel嘗試此操作時出現錯誤。 請檢查這張圖片。 在此處輸入圖片說明

我嘗試使用郵遞員生成以下圖像的代碼段 在此處輸入圖片說明

只需使用內容類型檢查響應格式,即可以正確的格式打印它。

 $contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
    echo $contentType;
    echo "<br/>";
  curl_close ($curl);
    echo "<pre>";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM