回调 URL 未接收 JSON 数据

Callback URL not receiving JSON data

提问人:Shadrack Kipkorir 提问时间:11/6/2021 最后编辑:Stephen OstermillerShadrack Kipkorir 更新时间:2/25/2023 访问量:606

问:

我有一个 PHP 页面,该页面接收 JSON 响应,位于 I have successfully posted responses using Postman when I post to either or h 但当我发布到https://example.ac.ke/op/api/mypesa/index.phphttps://example.com/op/api/mypesa/ttps://example.com/op/api/mypesa/index.phphttps://example.com/op/api/mypesa

我尝试在 htaccess 中重定向和添加尾随 / 徒劳无功。 我需要成为我的回调 URL。当我发布时,调用了接收页面,但似乎数据没有被重定向。https://example.com/op/api/mypesa

需要帮助重定向页面和帖子数据由于我成功重定向了页面但没有重定向数据

我的 .htaccess 包含以下内容

<IfModule mod_rewrite.c>

     RewriteEngine On
     RewriteCond %{REQUEST_FILENAME}  -f [OR]
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteRule ^(.*)$ index.php [L,QSA]

</IfModule>

使用邮递员发帖

Posting using postman to the page https://example.com/op/api/mypesa//

从页面接收 JSON 数据的数据库表

The database table receiving the json data from page

使用邮递员向页面发布

  • Posting using postman to the page https://example.com/op/api/mypesa

我的索引文件的内容位于 mypesa 文件夹中

<?php
   require 'config.php';
    header("Content-Type: application/json");

    $response = '{
        "ResultCode": 0, 
        "ResultDesc": "Confirmation Received Successfully"
    }';

    $mpesaResponse = file_get_contents('php://input');

    $logFile = "M_PESAConfirmationResponse.txt";

    $jsonMpesaResponse = json_decode($mpesaResponse, true); 

    $transaction = array(
            ':TransactionType'      => $jsonMpesaResponse['TransactionType'],
            ':TransID'              => $jsonMpesaResponse['TransID'],
            ':TransTime'            => $jsonMpesaResponse['TransTime'],
            ':TransAmount'          => $jsonMpesaResponse['TransAmount'],
            ':BusinessShortCode'    => $jsonMpesaResponse['BusinessShortCode'],
            ':BillRefNumber'        => $jsonMpesaResponse['BillRefNumber'],
            ':InvoiceNumber'        => $jsonMpesaResponse['InvoiceNumber'],
            ':OrgAccountBalance'    => $jsonMpesaResponse['OrgAccountBalance'],
            ':ThirdPartyTransID'    => $jsonMpesaResponse['ThirdPartyTransID'],
            ':MSISDN'               => $jsonMpesaResponse['MSISDN'],
            ':FirstName'            => $jsonMpesaResponse['FirstName'],
            ':MiddleName'           => $jsonMpesaResponse['MiddleName'],
            ':LastName'             => $jsonMpesaResponse['LastName']
    );

  
    $log = fopen($logFile, "a");
    fwrite($log, $mpesaResponse);
    fclose($log);

    echo $response;

 
    insert_response($transaction);
?>
json .htaccess 回调 终结点 mpesa

评论

0赞 kiner_shah 11/6/2021
当帖子不起作用时会发生什么?您是否收到错误消息?如果有任何与此相关的代码,请编辑帖子并包含它。
0赞 Shadrack Kipkorir 11/7/2021
我没有收到任何错误。只是当我使用 xxxxx.com/op/api/mypesa 温无法从 php 文件中读取数据,但是当我使用 xxxxx.com/op/api/mypesa 时,我可以从文件中读取并带到数据库
0赞 kiner_shah 11/7/2021
两个 URL 似乎相同。
0赞 Shadrack Kipkorir 11/7/2021
对不起,第二个有一个/即 xxxxx.com/op/api/mypesa/
0赞 kiner_shah 11/7/2021
两个 URL 仍然相同,具有 a 不起作用。但这是给定的,服务器处理请求的方式与没有 .如果您拥有服务器端代码,则需要添加一个包含的其他路由。////

答:

0赞 Joe Mwangi 2/25/2023 #1

确保您的回调 url 使用 https 从 safaricom API 获取响应。可以使用 ngrok!以在 Internet 上传输您的应用程序。 如果您已经这样做了并且您使用的是 ubuntu,您可以禁用 ufw 防火墙并重新启动计算机。sudo ufw disable