2016-03-18 66 views
0

好的,所以我想用一個表單創建一個簡單的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提供一個空白頁

任何想法?

+0

你是否在post.php頁面上獲得發佈數據? – Indrajit

+0

你期望收到什麼東西?嘗試在某處返回結果。 – ArchLicher

回答

1

嘗試var_dump($result);並看看返回的是什麼。我不認爲你實際上輸出任何東西,因此空白頁

+0

好吧,事實證明我有一個壞的令牌,導致服務器端的請求失敗。我現在可以收到推送,只有問題,它是空的。 我的猜測是從表單傳遞的變量有問題。 – Omran

+0

也許,確保你的表單將所有必需的變量傳遞給post.php。你可以通過var_dump($ _ POST)來檢查;在post.php腳本的開頭 – Vilius

+0

工作!非常感謝你! – Omran