简体   繁体   English

使用PHP + cURL的HTTP POST请求

[英]HTTP POST Request with PHP + cURL

I am trying to create HTTP post request using PHP + cURL that is equivalent to the example mentioned here of file "SendToast.aspx.cs" 我正在尝试使用PHP + cURL创建HTTP post请求,这相当于此处提到的文件“SendToast.aspx.cs”的示例

My PHP file looks like this below, 我的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);

?> ?>

can anyone tell me what im doing wrong here?, i also tried to use HTTPRequest Class to create this example, but i had hard time configuring php_http.dll extension, it seems the file has either dependencies or its corrupt. 任何人都可以告诉我我在这里做错了什么?,我也尝试使用HTTPRequest类来创建这个例子,但我很难配置php_http.dll扩展,似乎该文件有依赖或其损坏。 nevertheless help on this one would be great. 尽管如此,对这一点的帮助会很棒。

You are setting the http header( $header_array ) in a wrong way. 您正在以错误的方式设置http标头( $header_array )。 Use this one. 使用这个。

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

If this doesn't work then change the text/xml into application/xml from above. 如果这不起作用,则将text/xml application/xml从上面更改为application/xml

try to USE urlencode and prepare the variable for POST first 尝试使用urlencode并首先为POST准备变量

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

and then use this variable 然后使用此变量

curl_setopt($ch, CURLOPT_POSTFIELDS,$postedfield);

and decode the posted value 并解码发布的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM