У меня есть ситуация здесь.В настоящее время я разработал веб, и мне нужно создать функцию для взаимодействия с API устройства.Теперь я застрял с 307 (временное перенаправление) .
Итак, процесс, во-первых, мне нужно сделать сеанс с функцией входа в систему.При успешном входе в систему он выдаст ответ 307 (временное перенаправление) , и в заголовке ответа есть расположение (sessionId).Итак, чтобы отправить еще один запрос, нам нужно следовать Location (sessionId).Когда я пытался в Postman всегда добиться успеха для функции входа в систему, чтобы получить Location (SessionId), но для POST другой запрос из этих Location иногда был успешным, а иногда и не успешным.Я пытался сделать это на PHP, чтобы логин всегда был успешным и получал Местоположение, но при повторном запросе POST он всегда заканчивался неудачей.
Вот функция XML для логина:
POST / HTTP/1.1
Host: 111.222.123.123:8001
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lgi="http://www.qwerty.org/func/LGI">
<soapenv:Header/>
<soapenv:Body>
<lgi:LGI>
<lgi:OPNAME>username</lgi:OPNAME>
<lgi:PWD>password</lgi:PWD>
</lgi:LGI>
</soapenv:Body>
</soapenv:Envelope>
Этозаголовок ответа от функции входа в систему:
HTTP/1.1 307 Temporary Redirect
Location: http://111.222.123.123:8001/00112233
Server: Qwerty web server
Content-Type: text/xml; charset="utf-8"
Content-Length: 411
00112233 - это местоположение (SessionId) для отправки POST другого запроса.Но опять же, если от почтальона, иногда успех, а иногда не успех.И если из PHP всегда не удается POST запрос после получения Location (SessionId)
Вот мой PHP-скрипт для Логин Функция:
$soapUrl = "http://111.222.123.123:8001/";
$xml_post_string1 = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lgi="http://www.qwerty.org/func/LGI">
<soapenv:Header/>
<soapenv:Body>
<lgi:LGI>
<lgi:OPNAME>username</lgi:OPNAME>
<lgi:PWD>password</lgi:PWD>
</lgi:LGI>
</soapenv:Body>
</soapenv:Envelope>';
$headers1 = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: http://www.qwerty.org/func/LGI",
"Content-length: ".strlen($xml_post_string1),
);
$url = $soapUrl;
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch1, CURLOPT_URL, $url);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch1, CURLOPT_TIMEOUT, 10);
curl_setopt($ch1, CURLOPT_POST, true);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $xml_post_string1);
curl_setopt($ch1, CURLOPT_HTTPHEADER, $headers1);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_VERBOSE, 1);
curl_setopt($ch1, CURLOPT_HEADER, 1);
$response1 = curl_exec($ch1);
$header_size = curl_getinfo($ch1, CURLINFO_HEADER_SIZE);
$header = substr($response1, 0, $header_size);
$break1= explode('/', $header);
$break2= preg_split('/[\s]+/', $break1[4]);
$sessionId = $break2[0];
Это мой запрос после получения Location / SessionId :
$soapUrlToken1 = "http://111.222.123.123:8001/";
$soapUrlToken2 = $soapUrlToken1.$sessionId ;
$xml_post_string2 = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sh="http://www.qwerty.org/func/SH_SUB">
<soapenv:Header/>
<soapenv:Body>
<sh:SH_SUB>
<sh:USER>USERNAME</sh:USER>
<sh:DETAIL>TRUE</sh:DETAIL>
</sh:SH_SUB>
</soapenv:Body>
</soapenv:Envelope>';
$headers2 = array(
"POST /$token HTTP/1.1",
"Host: 111.222.123.123:8001",
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Accept-Encoding: gzip,deflate",
"User-Agent: City Commons-HttpClient/3.1",
"SOAPAction: http://www.qwerty.org/func/SH_SUB",
"Content-length: ".strlen($xml_post_string2),
);
$urlToken = $soapUrlToken2;
//echo "<br />".$urlToken."<br />";
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch2, CURLOPT_URL, $urlToken);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch2, CURLOPT_TIMEOUT, 10);
curl_setopt($ch2, CURLOPT_POST, true);
curl_setopt($ch2, CURLOPT_POSTFIELDS, $xml_post_string2);
curl_setopt($ch2, CURLOPT_HTTPHEADER, $headers2);
$response2 = curl_exec($ch2);
$response3 = str_replace("<SOAP-ENV:Body>","",$response2);
$response4 = str_replace("</SOAP-ENV:Body>","",$response3);
$parser2 = simplexml_load_string($response4);
В php ответ всегда о sessionId недействителен или истек тайм-аут.Я уже спрашиваю у команды API, они объяснили только о Сессия и порт должны поддерживать, чтобы сделать еще один запрос после входа в систему
Так что является лучшим решением для решения 307 (временное перенаправление) в PHP?
Спасибо