为什么我的CURLOPT_POSTFIELDS数据没有传递到服务器?

jjjwad0x  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(197)

我做了一个简单的api,它返回一些值,但是如果我添加这一行并传递像curl_setopt($cURLConnection, CURLOPT_POSTFIELDS , $data);这样的数据,我的api返回null,这是完整的代码

$data = ['a' => "data"];

        $url = env('MOTHERSHIP_URL') . '/store/cstore/receipt';
        Log::debug("url=" . $url);
        $cURLConnection = curl_init();
        curl_setopt($cURLConnection, CURLOPT_URL, $url);
        curl_setopt($cURLConnection, CURLOPT_POST, true);
        curl_setopt($cURLConnection, CURLOPT_POSTFIELDS , $data);
        curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($cURLConnection, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($cURLConnection, CURLOPT_SSL_VERIFYHOST, false);
                    
        $apiResponse = curl_exec($cURLConnection);
        curl_close($cURLConnection);
        $data = json_decode($apiResponse, true);

        dd($data);

路由

Route::any('/store/cstore/receipt', 'CStoreController@StoreCstoreReceipt')->name('store.cstore.receipt');

控制器

public function StoreCstoreReceipt(Request $request){

         Log::debug($request->all());
        return "Success";
     }

问题是如果我添加curl_setopt($cURLConnection, CURLOPT_POSTFIELDS , $data); api返回null,如果不添加此行,那么它工作正常,我做错了什么?我也尝试了其他方法,但在此我得到一些html接受获得响应

k3fezbri

k3fezbri1#

尝试curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($arr));

相关问题