Gravity Forms 在 xml 提交后将条目数据发送给第三方
Gravity Forms sending entry data to third-party after submitting in xml
我对 php/curl 和重力钩子还很陌生。我有 5 种形式,我正在尝试设置以使用重力形式 gform_after_submission 挂钩,以便它向第三方发送 xml 数据(sbs 网络服务 运行 sql 服务器).
这是我已经拥有的。
add_action('gform_after_submission_73', 'post_to_third_party', 10, 2);
function post_to_third_party($entry, $form) {
$post_url = 'http://mywebsite.co.uk/customers?clientid=1599999&secret=123';
$body = array(
'brand' => $entry['20'],
'product' => $entry['22'],
'form_id' => $entry['21'],
'title' => $entry['24'],
'fname' => $entry['23'],
'lname' => $entry['17'],
'postcode' => $entry['14'],
'address1' => $entry['2.1'],
'address2' => $entry['2.2'],
'town' => $entry['2.3'],
'county' => $entry['2.4']
);
$xml = '
<?xml version="1.0" encoding="WINDOWS-1252"?>
<webform>
<brand>$brand</brand>
<product>$product</product>
<form_id>$form_id</form_id>
<title>$title</title>
<fname>$fname</fname>
<lname>$lname</lname>
<postcode>$postcode</postcode>
<address1>$address1</address1>
<address2>$address2</address2>
<town>$town</town>
<county>$county</county>
</webform>';
var_dump($xml);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
echo $output;
curl_close($ch);
}
您的代码有几个问题,体面的 IDE 会立即告诉您。
function post_to_third_party($entry, $form)
$form
从来没有用过,所以省略它。
$body = array(...);
您从未使用 $body
,但稍后尝试访问条目 'directly'。
$xml = '...';
不插入单引号字符串(即不替换变量),正如 var_dump($xml);
已经向您展示的那样。
此外,您不会转义值,这些值可能包含 XML.
中不允许的字符
$ch = curl_init($url);
$url
不存在。您要使用的值在 $post_url
.
中
您正在为 cURL
使用不安全的设置,使您的代码容易受到 MITM 攻击(SSL 服务器欺骗)。
有关详细信息,请参阅 this documentation。
您没有任何用于错误检查的表格。
解决问题后,您的代码如下所示,现在应该可以工作了:
add_action('gform_after_submission_73', 'post_to_third_party', 10, 2);
function post_to_third_party($entry)
{
$url = 'http://mywebsite.co.uk/customers?clientid=1599999&secret=123';
$encoding = 'WINDOWS-1252';
$brand = htmlspecialchars($entry['20'], ENT_XML1, $encoding);
$product = htmlspecialchars($entry['22'], ENT_XML1, $encoding);
$form_id = htmlspecialchars($entry['21'], ENT_XML1, $encoding);
$title = htmlspecialchars($entry['24'], ENT_XML1, $encoding);
$fname = htmlspecialchars($entry['23'], ENT_XML1, $encoding);
$lname = htmlspecialchars($entry['17'], ENT_XML1, $encoding);
$postcode = htmlspecialchars($entry['14'], ENT_XML1, $encoding);
$address1 = htmlspecialchars($entry['2.1'], ENT_XML1, $encoding);
$address2 = htmlspecialchars($entry['2.2'], ENT_XML1, $encoding);
$town = htmlspecialchars($entry['2.3'], ENT_XML1, $encoding);
$county = htmlspecialchars($entry['2.4'], ENT_XML1, $encoding);
$xml = "<?xml version=\"1.0\" encoding=\"$encoding\"?>
<webform>
<brand>$brand</brand>
<product>$product</product>
<form_id>$form_id</form_id>
<title>$title</title>
<fname>$fname</fname>
<lname>$lname</lname>
<postcode>$postcode</postcode>
<address1>$address1</address1>
<address2>$address2</address2>
<town>$town</town>
<county>$county</county>
</webform>";
$ch = curl_init($url);
if ($ch === false) {
throw new RuntimeException("Unable to initialise a session");
}
$result = curl_setopt_array($ch, [
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => ['Content-Type: text/xml'],
CURLOPT_POSTFIELDS => $xml,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSL_VERIFYPEER => 1,
]);
if ($result === false) {
throw new RuntimeException("Unable to set session options");
}
$output = curl_exec($ch);
if ($output === false) {
throw new RuntimeException("Request failed: " . curl_error($ch));
}
curl_close($ch);
echo $output;
}
我对 php/curl 和重力钩子还很陌生。我有 5 种形式,我正在尝试设置以使用重力形式 gform_after_submission 挂钩,以便它向第三方发送 xml 数据(sbs 网络服务 运行 sql 服务器).
这是我已经拥有的。
add_action('gform_after_submission_73', 'post_to_third_party', 10, 2);
function post_to_third_party($entry, $form) {
$post_url = 'http://mywebsite.co.uk/customers?clientid=1599999&secret=123';
$body = array(
'brand' => $entry['20'],
'product' => $entry['22'],
'form_id' => $entry['21'],
'title' => $entry['24'],
'fname' => $entry['23'],
'lname' => $entry['17'],
'postcode' => $entry['14'],
'address1' => $entry['2.1'],
'address2' => $entry['2.2'],
'town' => $entry['2.3'],
'county' => $entry['2.4']
);
$xml = '
<?xml version="1.0" encoding="WINDOWS-1252"?>
<webform>
<brand>$brand</brand>
<product>$product</product>
<form_id>$form_id</form_id>
<title>$title</title>
<fname>$fname</fname>
<lname>$lname</lname>
<postcode>$postcode</postcode>
<address1>$address1</address1>
<address2>$address2</address2>
<town>$town</town>
<county>$county</county>
</webform>';
var_dump($xml);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
echo $output;
curl_close($ch);
}
您的代码有几个问题,体面的 IDE 会立即告诉您。
function post_to_third_party($entry, $form)
$form
从来没有用过,所以省略它。$body = array(...);
您从未使用
$body
,但稍后尝试访问条目 'directly'。$xml = '...';
不插入单引号字符串(即不替换变量),正如
var_dump($xml);
已经向您展示的那样。 此外,您不会转义值,这些值可能包含 XML. 中不允许的字符
$ch = curl_init($url);
$url
不存在。您要使用的值在$post_url
. 中
您正在为
cURL
使用不安全的设置,使您的代码容易受到 MITM 攻击(SSL 服务器欺骗)。 有关详细信息,请参阅 this documentation。您没有任何用于错误检查的表格。
解决问题后,您的代码如下所示,现在应该可以工作了:
add_action('gform_after_submission_73', 'post_to_third_party', 10, 2);
function post_to_third_party($entry)
{
$url = 'http://mywebsite.co.uk/customers?clientid=1599999&secret=123';
$encoding = 'WINDOWS-1252';
$brand = htmlspecialchars($entry['20'], ENT_XML1, $encoding);
$product = htmlspecialchars($entry['22'], ENT_XML1, $encoding);
$form_id = htmlspecialchars($entry['21'], ENT_XML1, $encoding);
$title = htmlspecialchars($entry['24'], ENT_XML1, $encoding);
$fname = htmlspecialchars($entry['23'], ENT_XML1, $encoding);
$lname = htmlspecialchars($entry['17'], ENT_XML1, $encoding);
$postcode = htmlspecialchars($entry['14'], ENT_XML1, $encoding);
$address1 = htmlspecialchars($entry['2.1'], ENT_XML1, $encoding);
$address2 = htmlspecialchars($entry['2.2'], ENT_XML1, $encoding);
$town = htmlspecialchars($entry['2.3'], ENT_XML1, $encoding);
$county = htmlspecialchars($entry['2.4'], ENT_XML1, $encoding);
$xml = "<?xml version=\"1.0\" encoding=\"$encoding\"?>
<webform>
<brand>$brand</brand>
<product>$product</product>
<form_id>$form_id</form_id>
<title>$title</title>
<fname>$fname</fname>
<lname>$lname</lname>
<postcode>$postcode</postcode>
<address1>$address1</address1>
<address2>$address2</address2>
<town>$town</town>
<county>$county</county>
</webform>";
$ch = curl_init($url);
if ($ch === false) {
throw new RuntimeException("Unable to initialise a session");
}
$result = curl_setopt_array($ch, [
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => ['Content-Type: text/xml'],
CURLOPT_POSTFIELDS => $xml,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSL_VERIFYPEER => 1,
]);
if ($result === false) {
throw new RuntimeException("Unable to set session options");
}
$output = curl_exec($ch);
if ($output === false) {
throw new RuntimeException("Request failed: " . curl_error($ch));
}
curl_close($ch);
echo $output;
}