douyu2817 2017-08-12 19:17
浏览 154
已采纳

使用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);

?>
  • 写回答

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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站