douyu2817
2017-08-12 19:17
浏览 139

使用cURL返回API XML响应

Forgive me if this is simple. My research has brought me to a halt at the moment. I am trying to use cURL to get the response of an API in XML.

This is the URL of the API: https://api.weather.gov/alerts/active/region/land

By default it returns in JSON. Which I know, I should just use the JSON response but, there is a reason I need it in XML as it will seamlessly integrate into my current code until I can rewrite it for JSON.

This is the documentation for the API. Under the API Reference tab is states I just need to change the request header to application/cap+xml. But I am not getting anything back. Just a blank white page.

https://alerts-v2.weather.gov/documentation

Here is my current code I am using to call the API but I get no response or anything. What am I missing?

    <?php

     $headers[] = 'Connection: Keep-Alive';
     $headers[] = 'Content-Type: application/cap+xml;charset=utf-8';
     $headers[] = 'Accept: application/cap+xml';

     $userAgent = 'php';

     $url = 'https://api.weather.gov/alerts/active/region/land';

     $cURL = curl_init();

     curl_setopt($cURL, CURLOPT_URL, $url);
     curl_setopt($cURL, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($cURL, CURLOPT_USERAGENT, $userAgent);
     curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($cURL, CURLOPT_HTTPGET, true);

     $result = curl_exec($cURL);

     curl_close($cURL);

?>

图片转代码服务由CSDN问答提供 功能建议

如果这很简单,请原谅我。 我的研究让我暂时停下来。 我正在尝试使用cURL来获取XML的API响应。

这是API的URL: https://api.weather.gov/alerts/active/region/land

默认情况下 以JSON返回。 我知道,我应该只使用JSON响应,但是有一个原因我需要它在XML中,因为它将无缝集成到我当前的代码中,直到我可以为JSON重写它。

这是API的文档。 在API Reference选项卡下的状态我只需要将请求标题更改为application / cap + xml。 但我没有收到任何回报。 只是一个空白的白页。

https://alerts-v2.weather.gov/documentation

这是我用来调用API的当前代码,但我没有得到任何回复。 我缺少什么?

 &lt;?php 
 
 $ headers [] ='Connection:Keep-Alive'; 
 $ headers [] ='Content- 键入:application / cap + xml; charset = utf-8'; 
 $ headers [] ='接受:application / cap + xml'; 
 
 $ userAgent ='php'; 
 
 $ url =  'https://api.weather.gov/alerts/active/region/land';

 $ cURL = curl_init(); 
 
 curl_setopt($ cURL,CURLOPT_URL,$ url); 
 curl_setopt(  $ cURL,CURLOPT_HTTPHEADER,$ headers); 
 curl_setopt($ cURL,CURLOPT_USERAGENT,$ userAgent); 
 curl_setopt($ cURL,CURLOPT_RETURNTRANSFER,true); 
 curl_setopt($ cURL,CURLOPT_HTTPGET,true); 
 
  $ result = curl_exec($ cURL); 
 
 curl_close($ cURL); 
 
?&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • duanjia9577 2017-08-12 19:52
    最佳回答

    I've just tried your code and it works fine with atom:

    $headers[] = 'Accept: application/atom+xml';
    

    "application/cap+xml" is not available for the URLs, like you mentioned in a question though.

    Per the doc https://alerts-v2.weather.gov/documentation the formats are following:

    /alerts/active/region/{region} => JSON-LD (default), ATOM 
    /alerts/{alertId}   => JSON-LD (default), CAP
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题