title: PHP Responding back to Facebook Webhook API
body: <p>Facebook made a recent change to Leadgen Webhooks where you've to respond with "200 OK" for each lead that comes in or they'll keep pinging you with that lead. Problem is, they don't mention if there's a way to do that and I've been trying to send a response but unsuccessfully so far.</p>
  
  <p>What I've tried so far:</p>
  
  <p><code>status_header(200);</code></p>
  
  <p><code>http_response_code(200);</code></p>
  
  <p><code>header[$_SERVER['SERVER_PROTOCOL'] . ' 200 OK']</code></p>
  
  <p><code>header[$_SERVER['SERVER_PROTOCOL'] . ' 200 OK', true, 200);</code></p>
  
  <pre><code>function handle_200() {
      status_header(200);
      header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK');
      error_log('test'); // This never even runs
  }
  add_action('send_headers', 'handle_200', 1);
  </code></pre>
  
  <p>None of the above has worked. I've also checked to ensure that at the point I was trying to change the status header, the current headers have not been sent by checking with <code>headers_sent()</code> and that returned a <code>FALSE</code>.</p>
  
  <p>I've also tried checking if the status header can be seen through either one of these:</p>
  
  <pre><code>error_log(print_r(getallheaders(), true));
  error_log(print_r(headers_list(), true));
  </code></pre>
  
  <p>But neither printed out the status header, but if I were to do something like <code>header('foo: bar')</code>, that would be seen in the headers list.</p>
  
  <p>I'm at a complete loss, any help to point in the right direction would be much appreciated.</p>
  
  <p>That being said, it might be relevant to mention that this script is running in the theme functions of a Wordpress Environment. Furthermore, the script itself (where I'm making these changes) is running within <code>ob_start()</code> and <code>ob_end_clean()</code> (not sure if relevant).</p>
  
  <p><strong>EDIT:</strong> I just found out that when I cause a Fatal Error that causes my script to end prematurely, Facebook receives a 200 OK response (ironically). This makes me think that the problem is because of running the script within <code>ob_start</code> and <code>ob_end_clean()</code>. Could that be delaying the header response?</p>
  
  <p><strong>EDIT 2:</strong> This remains unresolved. I've a temporary solution which successfully sends a response 35% of the time. I'm at a point where I get a lead from the webhook, retrieve the info from the lead and then do a SQL query to check if that lead has been received before, if it has, <code>exit(200)</code>. As simple as that, how can that take time <em>for leads that already exist</em>? It wouldn't! Yet my server (according to Facebook's Ad tool) still times out (without a response) 50% of the time? Something doesn't add up.</p>