phprelative-urlhttp-request2

Make relative url get request with HTTP_Request2


On the client side I have a get request called by $.get with jQuery

"modify_user.php?uid=SomeUserId"

with this XMLHttpRequest, I get some userID and load the form. The problem is that in the client side I do not have my user id, in case I want to modify my user.

So I create another php file called

modify_my_user.php

and basically what I wanted to do was get the id in the server side and simulate a request from server, like it was an Ajax request.

Something like this

require_once 'HTTP/Request2.php';


$r = new HTTP_Request2('Ajax/modify_user.php?uid='.$_SESSION['uid'], HTTP_Request2::METHOD_GET);
$r->setHeader(array('X-Requested-With' => 'XMLHttpRequest'));
$r->send();

echo json_encode($r->getBody());

this is because in the modify_user I have a reject method if the request isn't a XMLHttpRequest

(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');

the thing is, when I try to make the get request from server(in modify_my_user.php) I'm getting this error

HTTP_Request2_LogicException: HTTP_Request2 needs an absolute HTTP(S) request URL, 'localhostWeb/Ajax/modify_user.php?uid=17' given in

Is there a way to enable relatives url with this package?


Solution

  • You cannot make HTTP requests without a host - never and nowhere.

    There are several solutions to your problem:

    1. If no uid is given to modify_user.php, simply use the one from the session automatically.
    2. In case the special uid "current_user" is given to modify_user.php, the one from the session is used instead
    3. Prepend $_SERVER["HTTP_HOST"] to your URL before requesting it.