我正在尝试执行cURL GET请求并使用响应来更新我的表,但得到以下错误:
TypeErrorIlluminateDatabaseEloquentBuilder::create():参数#1 ($attributes)必须是array类型,字符串给定,在/home/vagrant/code/bp/vendor/laravel/framework/src/Illuminate/support/traits/forwardscalls .php第23行
通过Postman的类似GET请求返回以下响应结构:
{
"results": [
{
"transaction": "List",
"records": [
{
"CONO": "100",
"LNCD": "EN",
"CUNO": "000040",
"CUNM": "Custonmer Name",
"CUA1": "1234 Test Road",
"CUA2": "Alaska",
"CUA3": "USA",
"CUA4": "Test",
},
控制器:M3customCrudController.php
class M3ImportController extends CrudController {
public function fetchcust() {
$url = "https://abc123.com";
//will be as an admin field in official version
$username = 'xxx';
$password = 'yyy';
//Curl
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_USERAGENT, 'SP connector');
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
$response = curl_exec($curl);
$info = curl_getinfo($curl);
$response = [
'headers' => substr($response, 0, $info["header_size"]),
'body' => substr($response, $info["header_size"]),
];
$mydata = $response['body'];
M3custom::create($mydata);
}
}
首先使用Http facade,而不是curl。
第二,你得到json(字符串),而不是在数组中转换它。
第三-你的模型结构与响应相同?我不这么认为。
代码可能是这样的,但可能需要转换以适合模型的结构
public function fetchcust() {
$url = "https://abc123.com";
//will be as an admin field in official version
$username = 'xxx';
$password = 'yyy';
$response = Http::withBasicAuth($username, $password)
->withUserAgent('SP connector')
->get($url)
->json('results.records');
M3custom::create($response);
}