Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting from cURL: HTTP_X_REQUESTED_WITH

Tags:

ajax

php

curl

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.

like image 452
StackOverflowNewbie Avatar asked Jun 12 '12 12:06

StackOverflowNewbie


2 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); 
like image 122
Benno Avatar answered Sep 20 '22 02:09

Benno


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'].

like image 43
riha Avatar answered Sep 21 '22 02:09

riha