我正在尝试使用curl对API进行休息调用。这是我目前的代码:
$service_url = '...';
$curl = curl_init($service_url);
$curl_post_data = array(
'nome' => $this->name,
'email' => $this->email,
'age' => $this->age
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response = curl_exec($curl);
var_dump( $curl_response );
我知道包含姓名,电子邮件和年龄的数组有信息,因为我回应了它们。
问题是当我var_dump $ curl响应时,我得到以下消息:
{"status":"NOK","errorCode":-1,"errorMessage":"No Parameters sent"}
关于发生了什么的任何想法?
答案 0 :(得分:0)
试试这个:
$service_url = '...';
$curl = curl_init($service_url);
$curl_post_data = array(
'nome' => $this->name,
'email' => $this->email,
'age' => $this->age
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS,http_build_query($curl_post_data)); // using http_query_builder
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // for receiving response from server
$curl_response = curl_exec($curl);
var_dump( $curl_response );
选项CURLOPT_RETURNTRANSFER
将让curl_exec()返回响应(可选的进一步解析。