Web服务PHP代码与cUrl进入laravel

时间:2016-04-27 22:28:53

标签: php web-services laravel curl

我有一个用PHP编写的简单Web服务,但由于我必须将它实现到laravel框架,我现在已经丢失了。我试图将代码放到控制器甚至blade.php文件中,但都不起作用。关于如何转换这个的任何提示?这是我的代码:

$query = "SELECT distinct gcm_registration_id from users";
$result = mysql_query($query);
$results = array();

while($line = mysql_fetch_array($result, MYSQL_ASSOC)){
    $registrationIds[] = $line['gcm_registration_id'];
}
$msg = array
(
    'message'   => 'This is a test',
    'title'     => 'This is a title. title',
    'subtitle'  => 'This is a subtitle. subtitle',
    'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
    'vibrate'   => 1,
    'sound'     => 1,
    'largeIcon' => 'large_icon',
    'smallIcon' => 'small_icon'
);
$fields = array
(
    'registration_ids'  => $registrationIds,
    'data'          => $msg
);

$headers = array
(
    'Authorization: key=' . 'my-authorization-key',
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );

单击按钮时会触发。代码是这样的:

$(document).on("click", "#stop_nomination", function(e){
    e.preventDefault();
    var election_id = $(this).data('id');
    $this = $(this);

    if(window.confirm("Are you sure you want to end nomination?")) {
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "{{ URL::route('json/stopNomination') }}",
            data: { election_id: election_id }, //PASS DATA 
            success: function(data){ // RECEIVE DATA
                alert(data.msg);
                if(data.error != 1) {
                    $("#stop_nomination").css("display", "none");
                    $("#start_nomination").css("display", "block");
                    $("#div-nomination").css("display", "none");
                    $("#div-election").css("display", "block");
                    $("#text-nomination").html("Completed");
                }
            }
        });
    }
});

据我所知,它在控制器上调用该函数,基本上可以查询并更新数据库中的一些信息。我尝试插入我的代码,但它不会工作。

更新 到目前为止,我已经找到了执行代码的方法。我做的是将代码放在php文件中,然后将URL放在$.ajax下。但由于$.ajax上的网址不能同时接受两个网址,我想要的是将php网址文件放入控制器功能。有人能告诉我如何在控制器函数中触发php url文件吗?

0 个答案:

没有答案