我试图调用一个Web服务并且在将json对象传递给curl_setopt($cSession, CURLOPT_POSTFIELDS, $patientJS)
时它正在工作但是发送的对象没有正确的结构子我现在传递一个数组并且我得到一个内部服务器错误。如果我打印curl_error()它会告诉我
警告:curl_error():1044不是有效的cURL句柄资源
$patient = array('nom' => $_POST['nom'],
'prenom' => $_POST['prenom'],
'login' => $_POST['email'],
'password' => $_POST['password'],
'datenaissance' => $_POST['datenaissance'],
'sexe' => $_POST['sexe']);
$patientJS = json_encode($patient);
$cSession = curl_init();
curl_setopt($cSession, CURLOPT_URL, 'http://medicitussrv.herokuapp.com/enrollPatient');
curl_setopt($cSession, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cSession, CURLOPT_POST, 1);
curl_setopt($cSession, CURLOPT_POSTFIELDS, $patientJS);
$result = curl_exec($cSession);
curl_close($cSession);
这可以工作,但会发送一个像
这样的对象{ '{"nom":"Preciado","prenom":"Rodriguez","login":"artur_preciado@hotmail.com","password":"Medicitus1","datenaissance":"2017-09-06","sexe":"homme"}': '' }
传递一个数组:
$patient = array('nom' => $_POST['nom'],
'prenom' => $_POST['prenom'],
'login' => $_POST['email'],
'password' => $_POST['password'],
'datenaissance' => $_POST['datenaissance'],
'sexe' => $_POST['sexe']);
$cSession = curl_init();
curl_setopt($cSession, CURLOPT_URL, 'http://medicitussrv.herokuapp.com/enrollPatient');
curl_setopt($cSession, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cSession, CURLOPT_POST, 1);
curl_setopt($cSession, CURLOPT_POSTFIELDS, $patient);
$result = curl_exec($cSession);
curl_close($cSession);
这将返回内部服务器错误
答案 0 :(得分:0)
使用http_build_query
在curl中发送数组时,它将创建url编码的字符串
$patient = array('nom' => $_POST['nom'],
'prenom' => $_POST['prenom'],
'login' => $_POST['email'],
'password' => $_POST['password'],
'datenaissance' => $_POST['datenaissance'],
'sexe' => $_POST['sexe']);
$cSession = curl_init();
curl_setopt($cSession, CURLOPT_URL, 'http://medicitussrv.herokuapp.com/enrollPatient');
curl_setopt($cSession, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cSession, CURLOPT_POST, 1);
curl_setopt($cSession, CURLOPT_POSTFIELDS, http_build_query($patient));