Home how to Call REST-API with post method in CURL from laravel 5.6?
Reply: 0

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

user2262
1#
user2262 Published in June 20, 2018, 10:43 pm

I am using laravel 5.6.I want to call REST-API with post method. where URL Parameter is JSON format. I simply called that API in Postman .where it is working. but when i try to call this Laravel Controller via CURL method then getting error

Array to string conversion

JSON Parameter is

{"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}]
        }
}

My Laravel code is

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

This is Sample Image that being call from Postman. when i call this API directly then it is returning 200.

but when i try this from laravel then getting error . please check this image.

I try to use postman Generate code Snippets that images is below

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.3286 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO