我在Case下创建了一些自定义字段:
字段:
ESN Number
Order Id
Product name
CallBack URL
在回拨网址字段中,我有类似的内容:
{url : "abc.com", requestBody :"<some params>"}
单击“保存”按钮时,必须在“回调网址”字段中指定的URL上传递/发布所有字段的值。
我有我的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中的
更新:我尝试过这样的做法,将表单数据发布到我的本地网址。
<?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号码:123
订单编号:1232121212
产品名称:测试
1
d:\ Ampps \ WWW \ Suite_dev \定制\模块\例\ TestClass.php:47:空
RES:
但我没有得到任何回应。
截至目前,我只获取从字段输入的值,但我需要将这些值发布到网址。
答案 0 :(得分:0)
将$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];
在接收方转储帖子var_dump($_POST['product-info']);
,它应包含产品信息。
<强>更新强>
我可能不太清楚。由于您要发布到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 );
}
}
}
查看您是否收到任何错误或其他信息。