【问题标题】:Passing form variables to curl request将表单变量传递给 curl 请求
【发布时间】:2016-03-18 11:15:38
【问题描述】:

好的,所以我正在尝试创建一个简单的 HTML 页面,其中包含一个使用 cURL 提交 pushbullet 请求的表单。

到目前为止,我的 HTML 看起来像这样。

<html>
<head>
<title>Omran's Push Application</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<form method="post" onSubmit="window.location='index.htm" action="post.php">
<p>
<label for="login">From:</label>
  <input type="text" name="title" id="login">
</p>
<p>
  <label for="message">Message:</label>
  <input type="text" name="body" id="password">
</p>
<p class="login-submit">
  <button type="submit" class="login-button">Login</button>
</p>
</form>
</body>
</html>

以下是 PHP/cURL 方面的内容。

<?php
//create array of data to be posted
$post_data['title'] = $_POST['title'];
$post_data['body'] = $_POST['body'];
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//create cURL connection
$curl_connection = curl_init('https://api.pushbullet.com/v2/pushes');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_HTTPHEADER, ['Access-Token: TOKEN_HERE']);
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
curl_close($curl_connection);
?>

但它不是给予。它只是在 post.php 处提供一个空白页面

有什么想法吗?

【问题讨论】:

  • 您是否在 post.php 页面上获取帖子数据?
  • 您希望在哪里收到任何东西?尝试在某处返回结果。

标签: php curl pushbullet


【解决方案1】:

试试var_dump($result); 看看会发生什么。我认为您实际上没有输出任何内容,因此是空白页

【讨论】:

  • 好吧,原来我有一个错误的令牌,导致服务器端的请求失败。我现在可以收到推送,唯一的问题,它是空的。我的猜测是从表单传递的变量有问题。
  • 也许,确保您的表单将所有必需的变量传递给 post.php。你可以通过 var_dump($_POST);在 post.php 脚本的开头
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-15
  • 1970-01-01
相关资源
最近更新 更多