Post 表单数据到 Suitecrm 中的回调 URL
Post form data to Call back URL in Suite CRM
我在 Case 下创建了几个自定义字段,例如:
字段:
ESN Number
Order Id
Product name
CallBack URL
在回调 URL 字段中,我有类似的内容:
{url : "abc.com", requestBody :"<some params>"}
单击保存按钮时,所有字段的值必须是 'CallBack URL' 字段中指定的 URL 上的 passed/posted。
我有我的 logic_hook.php 喜欢:
$hook_array['after_save'][] = array(
2, //Integer
'CPE Closed URL callback', //String
'custom/modules/Cases/TestClass.php', //String or null if using namespaces
'TestClass', //String
'methodName', //String
);
在TestClass.php
UPDATED :我试过这样的方法,将 post 表单数据发送到我的本地 URL.
<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class TestClass
{
protected static $fetchedRow = array();
function methodName($bean, $event, $arguments)
{
if($bean->fetched_row['type']=='CPE' && $bean->fetched_row['state'] == 'Closed'){
//$json_call_back_url=$bean->callback_url_c;
$json_call_back_url='https://httpbin.org/get';
echo '<pre>'.print_r('ESN number:'.$bean->esn_number_c.'<br>'.'Order ID:'.$bean->order_id_c.'<br>'.'Product Name:'.$bean->product_name_c);
print_r(json_decode($json_call_back_url, true));
$myvars = ['product-info' => $bean->esn_number_c . '<br>' . $bean->order_id_c . '<br>' . $bean->product_name_c];
$ch = curl_init( $json_call_back_url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
var_dump($_POST['product-info']);
print_r('Res:'.$_POST['product-info']);die;
//die;
}
}
}
更新输出:
ESN number:123
Order ID:1232121212
Product Name:test
1
D:\Ampps\www\Suite_dev\custom\modules\Cases\TestClass.php:47:null
Res:
但我没有收到任何回复。
截至目前,我只获得从字段中输入的值,但我需要 post 这些值到 URL。
当您将 $json_call_back_url
传递给 curl_init
时,确保它实际上是 URL (http://example.com
) 而不是 json。你的代码print_r(json_decode($json_call_back_url, true));
让我感觉还是json
。此外,当您发送 $myvars
使用关联数组时:
$myvars = ['product-info' => $bean->esn_number_c . '<br>' . $bean->order_id_c . '<br>' . $bean->product_name_c];
在接收方转储 post var_dump($_POST['product-info']);
它应该包含产品信息。
更新:
我可能不够清楚。由于您正在 posting 到 https://httpbin.org/get
(接收方),因此您无法控制它。你只能在你能控制的接收方做var_dump($_POST['product-info']);
。试试这个:
class TestClass
{
protected static $fetchedRow = array();
function methodName($bean, $event, $arguments)
{
if($bean->fetched_row['type']=='CPE' && $bean->fetched_row['state'] == 'Closed'){
//$json_call_back_url=$bean->callback_url_c;
$json_call_back_url='https://httpbin.org/get';
echo '<pre>'.print_r('ESN number:'.$bean->esn_number_c.'<br>'.'Order ID:'.$bean->order_id_c.'<br>'.'Product Name:'.$bean->product_name_c);
$myvars = ['product-info' => $bean->esn_number_c . '<br>' . $bean->order_id_c . '<br>' . $bean->product_name_c];
$ch = curl_init( $json_call_back_url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
var_dump($response);
// Check if any error occurred
if (!curl_errno($ch)) {
$info = curl_getinfo($ch);
var_dump($info);
} else {
echo 'Curl error: ' . curl_error($ch);
}
curl_close( $ch );
}
}
}
查看您是否收到任何错误或其他信息。
我在 Case 下创建了几个自定义字段,例如:
字段:
ESN Number
Order Id
Product name
CallBack URL
在回调 URL 字段中,我有类似的内容:
{url : "abc.com", requestBody :"<some params>"}
单击保存按钮时,所有字段的值必须是 'CallBack URL' 字段中指定的 URL 上的 passed/posted。
我有我的 logic_hook.php 喜欢:
$hook_array['after_save'][] = array(
2, //Integer
'CPE Closed URL callback', //String
'custom/modules/Cases/TestClass.php', //String or null if using namespaces
'TestClass', //String
'methodName', //String
);
在TestClass.php
UPDATED :我试过这样的方法,将 post 表单数据发送到我的本地 URL.
<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class TestClass
{
protected static $fetchedRow = array();
function methodName($bean, $event, $arguments)
{
if($bean->fetched_row['type']=='CPE' && $bean->fetched_row['state'] == 'Closed'){
//$json_call_back_url=$bean->callback_url_c;
$json_call_back_url='https://httpbin.org/get';
echo '<pre>'.print_r('ESN number:'.$bean->esn_number_c.'<br>'.'Order ID:'.$bean->order_id_c.'<br>'.'Product Name:'.$bean->product_name_c);
print_r(json_decode($json_call_back_url, true));
$myvars = ['product-info' => $bean->esn_number_c . '<br>' . $bean->order_id_c . '<br>' . $bean->product_name_c];
$ch = curl_init( $json_call_back_url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
var_dump($_POST['product-info']);
print_r('Res:'.$_POST['product-info']);die;
//die;
}
}
}
更新输出:
ESN number:123
Order ID:1232121212
Product Name:test
1
D:\Ampps\www\Suite_dev\custom\modules\Cases\TestClass.php:47:null
Res:
但我没有收到任何回复。
截至目前,我只获得从字段中输入的值,但我需要 post 这些值到 URL。
当您将 $json_call_back_url
传递给 curl_init
时,确保它实际上是 URL (http://example.com
) 而不是 json。你的代码print_r(json_decode($json_call_back_url, true));
让我感觉还是json
。此外,当您发送 $myvars
使用关联数组时:
$myvars = ['product-info' => $bean->esn_number_c . '<br>' . $bean->order_id_c . '<br>' . $bean->product_name_c];
在接收方转储 post var_dump($_POST['product-info']);
它应该包含产品信息。
更新:
我可能不够清楚。由于您正在 posting 到 https://httpbin.org/get
(接收方),因此您无法控制它。你只能在你能控制的接收方做var_dump($_POST['product-info']);
。试试这个:
class TestClass
{
protected static $fetchedRow = array();
function methodName($bean, $event, $arguments)
{
if($bean->fetched_row['type']=='CPE' && $bean->fetched_row['state'] == 'Closed'){
//$json_call_back_url=$bean->callback_url_c;
$json_call_back_url='https://httpbin.org/get';
echo '<pre>'.print_r('ESN number:'.$bean->esn_number_c.'<br>'.'Order ID:'.$bean->order_id_c.'<br>'.'Product Name:'.$bean->product_name_c);
$myvars = ['product-info' => $bean->esn_number_c . '<br>' . $bean->order_id_c . '<br>' . $bean->product_name_c];
$ch = curl_init( $json_call_back_url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
var_dump($response);
// Check if any error occurred
if (!curl_errno($ch)) {
$info = curl_getinfo($ch);
var_dump($info);
} else {
echo 'Curl error: ' . curl_error($ch);
}
curl_close( $ch );
}
}
}
查看您是否收到任何错误或其他信息。