php - 通过 PHP POST 执行 2 个操作的按钮

标签 php html function button

我有这个执行两个功能的 PHP 脚本:

  1. 将 prayer_warriors 的 XML 标记中的计数增加 1。
  2. 向适当的用户发送推送通知。

在我的应用程序中,我可以 POST,脚本的所有功能都得到执行。脚本代码为:

<?php
$first_name = $_POST['first_name'];
 $last_name = $_POST['last_name'];
 $title = $_POST['title'];
 $deviceToken = $_POST['iostoken'];
$xml = simplexml_load_file("/Test.xml") or die("Not loaded!\n");


$responses = $xml->xpath("//channel/item[title = '$title' and first_name = '$first_name' and last_name = '$last_name']/prayer_warriors");
$responses[0][0] = $responses[0] + 1;
echo($responses);
$xml->asXML("Test.xml");  
// Put your device token here (without spaces):

// Put your private key's passphrase here:
$passphrase = 'Passphrase';

// Put your alert message here:
$message = 'Someone just pledged to pray for you!';

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
    'ssl://gateway.push.apple.com:2195', $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default'
    );

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);  
?>

我使用以下代码从网页发布到此 PHP。它会触发发送推送通知的部分,但不会将 prayer_warrior 计数增加 1。

$title = $item->title;
$first_name = $item->first_name;
$last_name = $item->last_name;
$iostoken = $item->iostoken;
Echo "<form action=\"http://Url.php\" method=\"post\">";
Echo "<input type=\"hidden\" name=\"first_name\" value=".$first_name.">";
Echo "<input type=\"hidden\" name=\"last_name\" value=".$last_name.">";
Echo "<input type=\"hidden\" name=\"title\" value=".$title.">";
Echo "<input type=\"hidden\" name=\"iostoken\" value=".$iostoken.">";

 Echo  "<input type=\"submit\" name=\"submit\" value=\"Pledge to pray\" />";
Echo "</form>";

知道为什么要这样做吗?

最佳答案

您写入的文件似乎与您正在读取的文件不同。

$xml = simplexml_load_file("/Test.xml") or die("Not loaded!\n");
                            ^

尝试改变

$xml->asXML("Test.xml");

$xml->asXML("/Test.xml");

在权限阻止您更新文件的情况下使用错误日志记录

if(false === $xml->asXML("/Test.xml")) {
  die('Could not update XML!');
}

关于php - 通过 PHP POST 执行 2 个操作的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23957396/

相关文章:

php - 当移动设备访问我的网络服务器时,如何获取它们的屏幕尺寸?

javascript - Angularjs:表单验证和输入指令

Html、Css、padding-top 不工作

javascript - 警报不配合输入

c - 正在读取文件,但未保存

javascript - 为 WordPress 中的多个菜单项提供属性(无 .js)

php - 建模产品库存变体电子商务与图像 PHP

php - 如何使用 PHP 在本地网络上遍历目录?

php - ElasticSearch-范围查询不起作用

php - mysql_fetch_array 不在函数内工作