使用wp_remote_get()代替无卷曲的caldav wordpress插件

时间:2018-11-13 09:00:50

标签: php wordpress curl caldav

我目前正在尝试编写一个wordpress插件,该插件可连接到caldav服务器并根据日历数据创建一个.ics文件。 第一步,我以https://uname.pingveno.net/blog/index.php/post/2016/07/30/Sample-public-calendar-for-ownCloud-using-ICS-parser作为起点,效果很好。 但是wordpress管理员拒绝接受使用普通卷曲的插件,他们建议通过WP http API https://developer.wordpress.org/plugins/http-api/完成所有操作。 我设法通过该API连接到caldav服务器,但无法获得所需的xml响应,只能得到纯HTML,该HTML不包含日历数据,而是一个.ics文件表,然后必须单独解析,不是很优雅... 问题似乎在于如何实现这三行代码:

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'REPORT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

通过wp API,尤其是:

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'REPORT');

这似乎无法通过wp_remote_get()或wp_remote_post()实现。 也许这里有人对我有暗示?

1 个答案:

答案 0 :(得分:0)

为了回答我自己的问题,解决方案是使用wp_remote_request()而不是wp_remote_get()或wp_remote_post(),因为我需要使用“报告”方法:

$args = array(
    'headers' => array(
    'Authorization' => 'Basic ' . base64_encode( $calendar_user . ':' 
    . $calendar_password ),
    'Content-Type' => 'application/xml; charset=utf-8',
    'Depth' => '1',
    'Prefer' => 'return-minimal'),
'method' => 'REPORT',
'body' => $body,
);
$response = wp_remote_request( $calendar_url, $args );

现在可以正常运行了。 也许这里的其他任何人都可以使用此...