我開發php應用程序,使用cURL向api服務器發送POST請求。在編碼之前,我用POSTMAN(Chorme Extension)測試了我的請求。我提出了2個請求,首先登錄第二個getSomeData。php cURL無效會話ID
https://serverip/ismserver/json/authentication
後數據
{
"username":"myName",
"password":"myPassword",
"domain":"domain"
}
https://serverip/ismserver/json/devicetemplate/getDeviceTemplates
Server保留我的會議和第二次請求的作品。 發佈數據
{
"templateRef":{
"refUid":"bc2929cc-5825-4948-8165-7f3fa8fea561",
"refName":"Sample Template",
"refObjectType":"vs_deviceTemplate",
"refVsomUid":"daf6f543-1841-4719-ab67-ff50ca8a7387"
}
}
這對郵差工作正常。
我開始編寫PHP代碼,這一次返回
{"status":{"errorType":"FAILURE","errorMsg":"Operation failed: SessionId is missing in the request header","errorReason":"SessionId is missing in the request header","errorReasonCode":"sessionid_not_in_header"}}
而且它不上RESTClient實現(Mozilla Firefox瀏覽器Extenision)
我的代碼是工作低於
//---- First Login --- //
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $server."/ismserver/json/authentication/login",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST =>false,
CURLOPT_COOKIESESSION => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\r\n \"username\":\"".$username."\",\r\n \"password\":\"".$password."\"\r\n}",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
),
));
//echo $str; //test string syntax
$response = curl_exec($curl);
preg_match('/^Set-Cookie:\s*([^;]*)/mi', $content, $m);
parse_str($m[1], $cookies);
$cookie = $cookies['NAMEOFCOOKIEUNEEDHERE'];
$err = curl_error($curl);
echo "</br>Login #:</br>";
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
//---- End -> First Login --- //
//--- Second GetCameras --- //
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL =>$server."/ismserver/json/camera/getCameras",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST =>false,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\r\n \"filter\":{\r\n \"includeEncoderVideoPortAssociationsOnly\":false,\r\n \"includeContactClosurePortAssociationsOnly\":false,\r\n \"byNotInRecommendedFirmwareVersion\":false,\r\n \"includeSubLocations\":false,\r\n \"byObjectType\":\"device_vs_camera\",\r\n \"pageInfo\":{\r\n \"start\":0,\r\n \"limit\":100\r\n }\r\n }\r\n}",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
echo "</br>GetCamerasr #:</br>";
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
//--- End -> GetCameras --- //
如何我解決這個問題。
非常感謝,也節省了我的一天:) –