u01jmg3 / ics-parser

Parser for iCalendar Events • PHP 8+, 7 (≥ 7.4), 5 (≥ 5.6)
MIT License
439 stars 144 forks source link

Outlook ICS failed to open stream: HTTP request failed! #311

Closed roelVerdonschot closed 2 years ago

roelVerdonschot commented 2 years ago

Description of the Issue:

When adding an outlook ICS link I get the PHP error failed to open stream: HTTP request failed! HTTP/1.1 400

Steps to Reproduce:

  1. Add outlook ICS link
  2. Run
u01jmg3 commented 2 years ago

Please fill in the issue template in full - I'm unable to investigate your issue without this

roelVerdonschot commented 2 years ago

What more do you need?

How to download ICal link?

  1. Create outlook account/Open https://outlook.live.com/calendar/
  2. In right top press the gear-icon
  3. Search for share
  4. Open Share agenda/Share calendar
  5. Copy link
gafderks commented 2 years ago

I had the same issue. It seems that Outlook is returning a 400 status because a User-Agent header is missing when the iCal is requested. I resolved this by downloading the iCal through cURL and then using initString.

$agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://outlook.office365.com/owa/calendar/...");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_USERAGENT, $agent);
$calendar_data = curl_exec($curl);
curl_close($curl);

$ical = new \ICal\ICal(false, [
    'defaultWeekStart' => 'MO'
]);
$ical->initString($calendar_data);
roelVerdonschot commented 2 years ago

Thx @gafderks

With the modification below or a way to set the userAgent via the constructor would be a better fix. protected $httpUserAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)';

u01jmg3 commented 2 years ago

You can already define a custom user agent: https://github.com/u01jmg3/ics-parser/commit/205afb3

roelVerdonschot commented 2 years ago

Ye but thats not via the constructor. The best fix would be is to add 'httpUserAgent' to the $configurableOptions. Or add the agent as default bc everybody that uses outlook links would need this.

u01jmg3 commented 2 years ago

Feel free to raise a PR

annervisser commented 2 years ago

The issue seems to be related to the Host header, not the user-agent header.

We've currently worked around this by downloading the file using Guzzle and then passing it to ICal with initString:

use GuzzleHttp\Client as GuzzleClient;
use ICal\ICal;

$iCalFile = (new GuzzleClient())->get($iCalUrl)->getBody()->getContents();
$iCal = new ICal(false, []);
$iCal->initString($iCalFile);
roelVerdonschot commented 2 years ago

@u01jmg3 I created a PR

SamLNL commented 1 year ago

I'm still receiving "failed to open stream: HTTP request failed! HTTP/1.1 400". Any updates on this issue?

u01jmg3 commented 1 year ago

The code is not yet released. Watch out for v3.2.0 or use dev-master in the meantime.

u01jmg3 commented 1 year ago

@SamLNL: see v3.2.0

SamLNL commented 1 year ago

@u01jmg3 thanks!