curl to cfhttp перевод

Я пытаюсь перевести скрипт PHP в ColdFusion и не удалось. Может ли кто-нибудь помочь мне?

Оригинальный скрипт PHP (для unifi wlan manager api):

$unifiServer = "https://xxx.xxx.xxx.xxx:xxxx";
$unifiUser = "xxxxx";
$unifiPass = "xxxxx";

// Start Curl for login
$ch = curl_init();
// We are posting data
curl_setopt($ch, CURLOPT_POST, TRUE);
// Set up cookies
$cookie_file = "/tmp/unifi_cookie";
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
// Allow Self Signed Certs
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// Force SSL1 only
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
// Login to the UniFi controller
curl_setopt($ch, CURLOPT_URL, "$unifiServer/api/login");
$data = json_encode(array("username" => $unifiUser,"password" => $unifiPass));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
// send login command
curl_exec ($ch);

ColdFusion cfhttp перевод:

<cfhttp url="https://xxx.xxx.xxx.xxx:8443/api/login" method="POST" result="test">
<cfhttpparam type = "formField" name = "username" value="xxxxxxx">
<cfhttpparam type = "formField" name = "password" value="xxxxxxxx">
<cfhttpparam type="header" name="Content-Type" value="application/json">
</cfhttp>

<cfdump var="#test#">

cfhttp не работает с:

«HTTP / 1.1 400 Bad Request Server»
msg «:» api.err.Invalid «,» rc «

SSL-сертификат установлен в хранилище сертификатов.

2

Решение

$unifiUser = "xxxxx";
$unifiPass = "xxxxx";
$data      = json_encode(array("username" => $unifiUser,"password" => $unifiPass));

переводит на

<cfset unifiUser = "xxxxx">
<cfset unifiPass = "xxxxx">
<cfset data      = serializeJSON({ "username" = unifiUser, "password" = unifiPass })>

и полезная нагрузка вашего локона переводится в

<cfhttp url="https://xxx.xxx.xxx.xxx:8443/api/login" method="POST" result="test">
<cfhttpparam type="body" value="#data#">
<cfhttpparam type="header" name="Content-Type" value="application/json">
</cfhttp>

<cfdump var="#test#">

Чтобы отправить куки:

<cfset cookie_file    = "/tmp/unifi_cookie">
<cfset cookie_content = fileRead(cookie_file)>

и добавить

<cfhttpparam type="header" name="Cookie" value="#cookie_content#">

Если вам нужно отправить несколько файлов cookie, вы должны объединить их в cookie_content используя точку с запятой ;,

2

Другие решения

если CFHTTP вызывает у вас головную боль (что часто случается при работе с SSL / HTTPS), рассмотрите возможность вызова CURL из командной строки, используя CFEXECUTE.

Например:

<cfexecute timeout="10" name="curl" variable="myVar" arguments="https://google.com"></cfexecute>
0