This article mainly introduces how to solve the problem that php cannot receive parameters. In daily operations, I believe that many people have doubts about how to solve the problem that php cannot receive parameters. The method of operation, I hope it will be helpful for everyone to answer the doubts about how to solve the problem that php cannot receive parameters! Next, please follow the editor to learn together!
Solutions for php not receiving parameters: 1. Modify the sender 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(...) That's it.
Specific problem description:
I wrote an interface with thinkphp.
Use curl to call the interface at the remote end. The curl is written as follows:
function curl($url,$method,$params,$auth){ //Initialize CURL handle $curl = curl_ init(); curl_ setopt($curl, CURLOPT_URL, $url);// Set the requested URL #curl_ setopt($curl, CURLOPT_HEADER, false);// Do not use http headers to speed up efficiency curl_ setopt($curl, CURLOPT_RETURNTRANSFER,1); // Set TRUE to curl_ The exec() result is converted to a string instead of being directly output //SSL authentication curl_ setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // The https request should be set to false and hosts FALSE to prevent cURL from verifying peer's certificate. The default value is TRUE from cURL 7.10. Start the default binding installation from cURL 7.10. curl_ setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);// Check whether there is a common name in the server SSL certificate. $header[] = "Content-Type:application/json;charset=utf-8"; if(!empty($header)){ curl_ setopt ( $curl, CURLOPT_HTTPHEADER, $header );// Set the array of HTTP header fields. Format: array ('Content type: text/plain ',' Content length: 100 ') } //Request time $timeout = 30; curl_ setopt ($curl, CURLOPT_CONNECTTIMEOUT, $timeout);// Set connection waiting time //Data submission of different request methods switch ($method){ case "GET" : curl_ setopt($curl, CURLOPT_HTTPGET, true);// If TRUE, the HTTP method will be set to GET. Because the default is GET, this option is only required when the method is modified. break; case "POST": if(is_array($params)){ $params = json_ encode($params,320); } #curl_ setopt($curl, CURLOPT_POST,true);// When TRUE, a POST request will be sent. The type is: application/x-www-form-urlencoded, which is the most common type of HTML form submission. #curl_ setopt($curl, CURLOPT_NOBODY, true);// When TRUE, the BODY part will not be output. Meanwhile, Mehtod becomes HEAD. When it is changed to FALSE, it will not become GET. curl_ setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");// When HTTP requests are made, a custom Method is used instead of "GET" or "HEAD". It is useful for "DELETE" or other more covert HTTP requests. Valid values include "GET", "POST", "CONNECT", etc; //Set submitted information curl_ setopt($curl, CURLOPT_POSTFIELDS,$params);// All data is sent using the "POST" operation in the HTTP protocol. 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);// Execute predefined CURL $status = curl_ getinfo($curl, CURLINFO_HTTP_CODE);// Get the http return value, the last received HTTP code curl_ close($curl);// Close cURL session $res = json_ decode($data,true); return $res; }
I found a problem, that is, when the post is submitted, the background cannot receive the incoming 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, I found that
$header[] = "Content-Type:application/ json;charset=utf-8";
and
if(is_array($params)){
$params = json_encode($params,320);
}
The data submitted is json type, and the form-data of the body is used to test the interface with postman
The correct solution code As follows:
//Sender
$data = array(
'openid' => "123",
'keyword' => "321"
);
$data = json_encode($data);
...
Curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//receiving end
$obj = file_get_contents("php://input");
$data = json_decode($obj, true);
Copyright Description:No reproduction without permission。
Knowledge sharing community for developers。
Let more developers benefit from it。
Help developers share knowledge through the Internet。
Follow us
2023-03-13
2023-03-13