Sanj
Sanj

Reputation: 141

Ajax not call when web-hook send some event

I have two pages one is workflow.php where web-hook send some data, after receiving data I want to send this data to other page because of some reason which is aq.php

workflow.php

<?
$cmd = 'echo "hii3" > debug2.log';
echo "<pre>".shell_exec($cmd)."</pre>";
$cmd1 = 'echo '.$_POST['action'].' >> debug2.log';
echo "<pre>".shell_exec($cmd1)."</pre>";
$cmd2 = 'echo ' .$_POST['id'].' >> debug2.log';
echo "<pre>".shell_exec($cmd2)."</pre>";
$id=$_POST['id'];
?>
<script type="text/javascript" 
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js">
</script>
<script type="text/javascript">
$(
function() {


var id= '<?php echo $id ;?>';
$.ajax({
    url: "http://35.160.133.54/bitrix/aq.php",
                    dataType: "json",
                    type: "POST",
                    data: {id : id},
                    success: function(data){


                                        }
                    }); 




});
</script>
<?require($_SERVER["DOCUMENT_ROOT"]."/bitrix/footer.php");?> 

aq.php

<?
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/header.php");
$APPLICATION->SetTitle("AQ");

if($_POST)
{
$cmd = "echo ".$_POST['id']." > debug5.log";
echo "<pre>".shell_exec($cmd)."</pre>";
}

?><?require($_SERVER["DOCUMENT_ROOT"]."/bitrix/footer.php");?>  

When I run workflow.php manually then its run the aq.php file.But when web-hook send some data to workflow.php its not run aq.php

Upvotes: 1

Views: 1105

Answers (1)

Webdesigner
Webdesigner

Reputation: 1982

A web-hook is not a Browser... so what ever you do in your browser will work because your browser is executing the JavaScript (the Ajax call). A web-hook only doing the (GET/POST) request, what ever you respond will NOT be executed! If you want do solve this you have to find an other way.

For example if you have the privilege on your sever to execute curl you can simulate the Ajax request.

'curl -i -H "Accept: application/json" -d "id":' . $id .' http://35.160.133.54/bitrix/aq.php'

Or you use the PHP version:

<?php                                                              
    $data_string = json_encode(array("id" => $id)); 
    $curl = curl_init();
    curl_setopt ($curl, CURLOPT_URL, "http://35.160.133.54/bitrix/aq.php");
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string))
    );
    curl_exec ($curl);
    curl_close ($curl);

Upvotes: 1

Related Questions