"123",'keyword' => "321");$data = json_encode($data); ..."; 2. Modify the receiving end code to "$obj=file_get_contents("php://input");$data=json_decode(...)"."/> "123",'keyword' => "321");$data = json_encode($data); ..."; 2. Modify the receiving end code to "$obj=file_get_contents("php://input");$data=json_decode(...)".">

Home>Article>Backend Development> What should I do if php cannot receive parameters?

What should I do if php cannot receive parameters?

藏色散人
藏色散人 Original
2023-02-09 10:16:31 4171browse

Solution to the problem that php cannot receive parameters: 1. Modify the sending code to "$data = array ('openid' => "123",'keyword' => "321");$ data = json_encode($data);..."; 2. Modify the receiving end code to "$obj=file_get_contents("php://input");$data=json_decode(...)".

What should I do if php cannot receive parameters?

The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer

What should I do if php cannot receive parameters? ?

  • curl submission parameter php cannot receive the problem

Specific problem description:

Write an interface using thinkphp.

Use curl to call the interface on the remote end. Curl is written as follows:

function curl($url,$method,$params,$auth){ //初始化CURL句柄 $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url);//设置请求的URL #curl_setopt($curl, CURLOPT_HEADER, false);// 不要http header 加快效率 curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出 //SSL验证 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求时要设置为false 不验证证书和hosts FALSE 禁止 cURL 验证对等证书(peer's certificate), 自cURL 7.10开始默认为 TRUE。从 cURL 7.10开始默认绑定安装。 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);//检查服务器SSL证书中是否存在一个公用名(common name)。 $header[] = "Content-Type:application/json;charset=utf-8"; if(!empty($header)){ curl_setopt ( $curl, CURLOPT_HTTPHEADER, $header );//设置 HTTP 头字段的数组。格式: array('Content-type: text/plain', 'Content-length: 100') } //请求时间 $timeout = 30; curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, $timeout);//设置连接等待时间 //不同请求方法的数据提交 switch ($method){ case "GET" : curl_setopt($curl, CURLOPT_HTTPGET, true);//TRUE 时会设置 HTTP 的 method 为 GET,由于默认是 GET,所以只有 method 被修改时才需要这个选项。 break; case "POST": if(is_array($params)){ $params = json_encode($params,320); } #curl_setopt($curl, CURLOPT_POST,true);//TRUE 时会发送 POST 请求,类型为:application/x-www-form-urlencoded,是 HTML 表单提交时最常见的一种。 #curl_setopt($curl, CURLOPT_NOBODY, true);//TRUE 时将不输出 BODY 部分。同时 Mehtod 变成了 HEAD。修改为 FALSE 时不会变成 GET。 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");//HTTP 请求时,使用自定义的 Method 来代替"GET"或"HEAD"。对 "DELETE" 或者其他更隐蔽的 HTTP 请求有用。 有效值如 "GET","POST","CONNECT"等等; //设置提交的信息 curl_setopt($curl, CURLOPT_POSTFIELDS,$params);//全部数据使用HTTP协议中的 "POST" 操作来发送。 break; case "PUT" : curl_setopt ($curl, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($curl, CURLOPT_POSTFIELDS,json_encode($params,320)); break; case "DELETE": curl_setopt ($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_setopt($curl, CURLOPT_POSTFIELDS,$params); break; } $data = curl_exec($curl);//执行预定义的CURL $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);//获取http返回值,最后一个收到的HTTP代码 curl_close($curl);//关闭cURL会话 $res = json_decode($data,true); return $res; }

I found a problem, that is, when the post is submitted, the background cannot receive the passed parameters.

Background reception:

public function getUserById(){ $custid = I("post.cust_uid"); $companyId = I("post.company_id"); $model = M('chatuser','snake_',$this->db); $mine = $model->field('username,avatar,sign,id')->where(['id'=>$custid])->find(); $company = $model->field('username,avatar,sign,id')->where(['id'=>$companyId])->find(); $return['mine'] =$mine; $return['company'] =$company; $return['status'] = 1; $this->ajaxReturn($return); }

After various debugging, it was found that

$header[] = "Content-Type:application/json;charset=utf-8";

and

if(is_array($params)){ $params = json_encode($params,320); }

in the header submitted json-type data, and using The postman test interface uses body's form-data

. Urgent, you can only comment out the above two codes first. Submit normally. When you have time, let’s take a look at the json receiving method.

If there are any classmates who know and want to leave a message to clear up any doubts, we will be grateful!

The code for the correct solution is as follows:

//发送端 $data = array ( 'openid' => "123", 'keyword' => "321" ); $data = json_encode($data); ... curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); //接受端 $obj = file_get_contents("php://input"); $data = json_decode($obj, true);

Recommended study: "PHP Video Tutorial"

The above is the detailed content of What should I do if php cannot receive parameters?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn