[英]HTTP POST Request with PHP + cURL
我正在尝试使用PHP + cURL创建HTTP post请求,这相当于此处提到的文件“SendToast.aspx.cs”的示例
我的PHP文件如下所示,
<?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扩展,似乎该文件有依赖或其损坏。 尽管如此,对这一点的帮助会很棒。
您正在以错误的方式设置http标头( $header_array
)。 使用这个。
$header_array = array(
'X-WindowsPhone-Target: toast',
'X-NotificationClass: 2',
'Content-type: text/xml'
);
如果这不起作用,则将text/xml
application/xml
从上面更改为application/xml
。
尝试使用urlencode
并首先为POST准备变量
$postedfield="MYPOST=".urlencode($theData);
然后使用此变量
curl_setopt($ch, CURLOPT_POSTFIELDS,$postedfield);
并解码发布的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.