首页 > 解决方案 > 使用 curl 将会话值传递给另一个项目,并以 "{"abc@gmail_com":null}" 这种格式接收该值

问题描述

在我的项目中第一次使用 curl。我在我的项目中使用了会话。当我这样做时,{{ dd(session()->all()) }}我得到了想要的结果 "email" => "abc@gmail.com"

但是当我通过 curl 将此变量传递给我的其他项目时,会以这种格式接收值"{"abc@gmail_com":null}"

.com 更改为 _com 并且有一个空值

因此,我无法根据该电子邮件地址从数据库中获取记录。以下是我的代码:

        $sendData = Session::get('email');
//        dd($sendData);
        $url = 'http://localhost/myproject/project-apis/public/index.php/myRequest';

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$url);
            curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$sendData);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $server_output = curl_exec($ch);
            curl_close ($ch);
    //        dd($server_output);
            $dataReceive = json_decode($server_output);
            if($dataReceive->status == 'TRUE'){
                $profile = $dataReceive->profile;
            }
            else{
                $profile = array();
            }

请帮帮我。

注意:我没有在这个项目中使用工匠服务,所以我的网址很好。

标签: phplaravelsessioncurl

解决方案


CURLOPT_POSTFIELDS期待一个数组或一个 urlencoded 字符串:

https://www.php.net/manual/en/function.curl-setopt.php

但是这一行仅将电子邮件地址存储在$sendData

$sendData = Session::get('email');

尝试CURLOPT_POSTFIELDS这样设置:

curl_setopt($ch, CURLOPT_POSTFIELDS, ['email' => $sendData]);

推荐阅读