0

I am trying to execute a cURL GET request and use the response to update my table, but getting the following error:

TypeError Illuminate\Database\Eloquent\Builder::create(): Argument #1 ($attributes) must be of type array, string given, called in /home/vagrant/code/bp/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php on line 23

A similar GET request through Postman returns the following response structure:

{
    "results": [
        {
            "transaction": "List",
            "records": [
                {
                    "CONO": "100",
                    "LNCD": "EN",
                    "CUNO": "000040",
                    "CUNM": "Custonmer Name",
                    "CUA1": "1234 Test Road",
                    "CUA2": "Alaska",
                    "CUA3": "USA",
                    "CUA4": "Test",
                   
                },

Controller: 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);

    }

}

1 Answer 1

1

First - use Http facade, not curl.

Second - you get json (string) and not converting it in array.

Third - your model structure is same as response? I don't think so.

Code may be like this, but it may be necessary to convert to fit the structure of the model

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.