Skip to main content
 首页 » 编程设计

c#之异步 HttpWebRequest 上的此 .net/C# 代码的 PHP 等效项、自定义 header 二进制有效负载和分析响应

2024年01月12日26insus

我正在寻找与以下代码等效的 PHP,即使它不是可编译的代码,只要提供用于执行这些功能的高级函数就很好了。


        string subscriptionUri = "sample.com"; 
        HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri); 
        sendNotificationRequest.Method = "POST"; 
 
        sendNotificationRequest.Headers.Add("X-MessageID", "<UUID>"); 
        sendNotificationRequest.ContentType = "text/xml"; 
 
 
        sendNotificationRequest.ContentLength = notificationMessage.Length; 
        byte[] notificationMessage = new byte[] {<payload>}; 
 
        using (Stream requestStream = sendNotificationRequest.GetRequestStream()) 
        { 
           requestStream.Write(notificationMessage, 0, notificationMessage.Length); 
        } 
 
        // Sends the notification and gets the response. 
        HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse(); 
        string notificationStatus = response.Headers["X-NotificationStatus"]; 
        string notificationChannelStatus = response.Headers["X-SubscriptionStatus"]; 
        string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];   

所以基本上我正在寻找 PHP 等同于以下内容

  1. 发送带有一些自定义 header 和内容类型的异步请求并将其发送为异步/流
  2. 从字符串中创建一个以字节为单位的有效载荷
  3. 获取响应并查看 header 。

请您参考如下方法: