2014-02-16 158 views
0

我試圖使用PHP +捲曲是相當於例子中提到的文件「SendToast.aspx.cs」HTTP POST請求與PHP +捲曲

我的PHP文件看起來像下面這樣here創建HTTP POST請求,

<?php 

$uri  = $_POST["uri"]; 
$title = $_POST["title"]; 
$subtitle = $_POST["subtitle"]; 


$file = 'file.txt'; 
//phpinfo(); 
$theData = '<?xml version="1.0" encoding="utf-8"?>\\r'; 
$theData .= "<wp:Notification xmlns:wp=\"WPNotification\">"; 
$theData .= "<wp:Toast>"; 
$theData .= "<wp:Text1>" .$title; 
$theData .= "</wp:Text1>"; 
$theData .= "<wp:Text2>" .$subtitle; 
$theData .= "</wp:Text2>"; 
$theData .= "<wp:Param>/Page2.xaml?NavigatedFrom=Toast Notification</wp:Param>"; 
$theData .= "</wp:Toast>"; 
$theData .= "</wp:Notification>"; 

$header_array = array('X-WindowsPhone-Target' => 'toast','X-NotificationClass' => '2','Content-type:' => 'text/xml','Content-length:' => strlen($theData)); 
$ch = curl_init(); 

curl_setopt($ch, CURLOPT_HTTPHEADER,$header_array); 
curl_setopt($ch, CURLOPT_URL,$uri); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,$theData); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
//print_r($ch); 
$server_output = curl_exec ($ch); 
//echo $server_outout; 
//curl_close ($ch); 

if (curl_errno($ch)) 
{ 
     print "Error: " . curl_error($ch); 
} 
else 
{ 
// Show me the result 
    print $server_output; 
    curl_close($ch); 
} 
//file_put_contents($file, $theData); 

?>

誰能告訴我什麼即時做錯了什麼?我還試圖用HttpRequest類來創建這個例子,但我很難配置php_http.dll延伸,似乎該文件具有依賴關係或其損壞。儘管如此,幫助這一個將是偉大的。

+0

請讓我們知道您遇到的錯誤或不正確的行爲。 –

+0

對不起哥們,但我不太熟悉php的輸出函數,我可以告訴的是,如果一切正常,它應該發送通知給我的手機應用程序(ASP示例完美工作,它發送通知,意味着phoneapp可以接收),server_output變量不會返回任何內容,其空白或可能是我不知道如何完全打印它,你能提示我如何打印錯誤代碼或響應數據?這將是非常有用的 – maxchirag

+0

你忘了網址編碼POST數據:curl_setopt($ ch,CURLOPT_POSTFIELDS,urlencode($ theData)); – hindmost

回答

2

要設置HTTP報頭( $header_array)以錯誤的方式。使用這個。

$header_array = array(
    'X-WindowsPhone-Target: toast', 
    'X-NotificationClass: 2', 
    'Content-type: text/xml' 
); 

如果不工作,然後從上面改變text/xmlapplication/xml

+0

是的,從我學習過的各種示例中得知。無論如何,謝謝你的回覆。 – maxchirag

0

嘗試使用urlencode和準備變量POST第一

$postedfield="MYPOST=".urlencode($theData); 

,然後使用這個變量

curl_setopt($ch, CURLOPT_POSTFIELDS,$postedfield); 

和解碼公佈值

+0

試過,沒有幫助:-( – maxchirag

+0

中斷首先嚐試不使用任何頭文件,然後嘗試輸出文本中的變量,如果正在輸出,則可能需要使用有效的xml格式來讓C#理解 –