【发布时间】:2018-05-16 20:47:44
【问题描述】:
我正在使用 laravel 5.6。我想用 post 方法调用 REST-API。其中 URL 参数为 JSON 格式。我只是在 Postman 中调用了该 API。它正在工作的地方。但是当我尝试通过 CURL 方法调用这个 Laravel Controller 然后得到 错误
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。
【问题讨论】:
-
当我添加 CURLOPT_POSTFIELDS => json_encode($data),然后从响应中获取 Null。