Home » Php » php – Posting from cURL: HTTP_X_REQUESTED_WITH

php – Posting from cURL: HTTP_X_REQUESTED_WITH

Posted by: admin July 12, 2020 Leave a comment


I’m programatically posting a form to a PHP form handling script. Is there a way for me to make the form handling script think that the post is being done by ajax? The form handler currently checks for HTTP_X_REQUESTED_WITH in $_SERVER to implement special ajax only logic. I need those codes executed when I post to the form using cURL.

How to&Answers:

via PHP cURL – http://www.php.net/manual/en/function.curl-setopt.php

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-With: XMLHttpRequest"));
$result = curl_exec ($ch);
curl_close ($ch); 


curl -H "X-Requested-With: XMLHttpRequest" ...

This sends the header “X-Requested-With” with your request. The value (“XMLHttpRequest”) is then available in $_SERVER['HTTP_X_REQESTED_WITH'].