2014-02-16 163 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#理解 –