PHP SSE -> How to make a proxy calling another SSE (Server Sent Events)

Viewed 45

I'm trying to find any way to create a proxy for Handle Server Sent Events from Facebook API (https://streaming-graph.facebook.com)

Because of CORS policy, I can't run my script localy so i'm trying to find any way to creat a proxy for that in PHP.

I tried many things but nothing work like I want. I would like to create a text/event-stream returning what i'm having from Facebook SSE.

<?php
  // Set file mime type event-stream
  header('Content-Type: text/event-stream');
  header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
  header("Access-Control-Allow-Origin: *");

  $mustQuit = false;

  // Server Sent events LINK
  $handle = fopen(...url..., "r");

  while (!$mustQuit && connection_status() == CONNECTION_NORMAL) {
    echo stream_get_contents($handle);
    flush();

    // Wait 2 seconds for the next message / event
    //sleep(1);  
  }

Nothing work ; it's only infinitly loading and sending nothing.

1 Answers

Solved !! I added http_response_code(200); in front of my file and everything work like a charm

Entier code :

<?php

  // Set file mime type event-stream
  http_response_code(200);
  header('Content-Type: text/event-stream');
  header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
  header("Access-Control-Allow-Origin: *");

  // prepare the url of the api I am calling
  $api_url = "https://streaming-graph.facebook.com/...";

  // Server Sent events LINK
  $handle = fopen($api_url, "r");

  while (connection_status() == CONNECTION_NORMAL) {
    echo stream_get_contents($handle);
    flush();
  }
Related