6

I am using the HttpRequest class in my php script, but when I uploaded this script to my hosting provider's server, I get a fatal error when executing it:

Fatal error: Class 'HttpRequest' not found in ... on line 87

I believe the reason is because my hosting provider's php.ini configuration doesnt include the extension that supports HttpRequest. When i contacted them they said that we cannot install the following extentions on shared hosting. So i want the alternative for httpRequest which i make like this:

   $url= http://ip:8080/folder/SuspendSubscriber?subscriberId=5
    $data_string="";
    $request = new HTTPRequest($url, HTTP_METH_POST);
    $request->setRawPostData($data_string);
    $request->send();    
    $response = $request->getResponseBody();
    $response= json_decode($response, true);
    return $response;

Or How can i use this request in curl as it is not working for empty datastring?

  • 1
    What is the point of sending an empty POST request? Why not just use GET? – Mike Apr 22 '14 at 06:50
  • 1
    @Mike There are indeed situations where this makes sense. For example if the server side logic acts different depending on the type of a request. This is the case in WEBDAV requests for example. – arkascha Apr 22 '14 at 06:54
  • Thats what exactly @arkascha is saying. the server side logic. –  Apr 22 '14 at 07:00
  • @arkascha Interesting. I wonder how the server even knows that POST was used if it receives an empty string. – Mike Apr 22 '14 at 07:00
  • @Mike Well because the request type is stated in the request headers. Simply take a look at a raw request and you will see. – arkascha Apr 22 '14 at 07:05
  • @arkascha Actually I don't think it is. I think it's just that POST data gets sent through the headers and GET is only sent by the request URI. – Mike Apr 22 '14 at 07:07
  • 1
    @Mike Fine if you think that, however this is simply wrong. Why didn't you take a short look as I suggested? It would have clearly shown this to you: example of a `GET` requests first header: `GET /posts/23212407/edit HTTP/1.1`. And here a `POST` request: `POST /posts/23212695/comments HTTP/1.1` IT is not a religion, you should _always_ distrust your own believes... – arkascha Apr 22 '14 at 07:14
  • @arkascha PHP's getallheaders() shows no such thing. Nowhere does it indicate whether it is GET or POST. – Mike Apr 22 '14 at 07:51
  • @arkascha However it does show up in phpinfo(). Strange. – Mike Apr 22 '14 at 07:54
  • @Mike When asked to take a look at a raw request, then why do you use a utility that filters and interprets such request to look at it? That certainly is not a look at the raw request. Anyways: you also could have taken a short look into wikipedia for this: http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Example_session – arkascha Apr 22 '14 at 08:14
  • @arkascha I assumed that the name "getallheaders()" and description on php.net were correct. – Mike Apr 22 '14 at 08:29
  • @Mike Ok, so... _did_ you take a look at a raw request in the mean time? – arkascha Apr 22 '14 at 08:34
  • @Mike Actually I have to admit that I was unprecise in my earlier comment. Sorry if this confused you: The request type is indeed specified in the first line of an http requests header section, however it is _not_ a header by itself, that might explain why phps `getallheaders()` does not list it. Instead RFC 2616 refers to that first line inside the headers section at "the request line", which is followed by header lines. Sorry for the confusion. – arkascha Apr 22 '14 at 08:37
  • @arkascha I was not able to look at a raw request because I could not find a way to do this with PHP and didn't think to look on Wikipedia. However thanks for clearing it up based on RFC 2616. That makes sense why it's not actually included in the headers. – Mike Apr 22 '14 at 18:00

4 Answers4

4

You can use CURL in php like this:

$ch = curl_init( $url );
$data_string  = " ";
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_string );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));   
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );   
$result = curl_exec($ch);
curl_close($ch);    
return $result;

and empty data string doesn't make sense in post request, but i've checked it with empty data string and it works quiet well.

Zahid Khan
  • 1,250
  • 3
  • 15
  • 31
2

you can use a framework like zend do this. framework usually have multiple adapters (curl,socket,proxy).

here is a sample with ZF2:

    $request = new \Zend\Http\Request();
    $request->setUri('[url]');
    $request->setMethod(\Zend\Http\Request::METHOD_POST);
    $request->getPost()->set('key', $value);

    $client = new \Zend\Http\Client();
    $client->setEncType('application/x-www-form-urlencoded');

    $response = false;
    try {
        /* @var $response \Zend\Http\Response */
        $response = $client->dispatch($request);
    } catch (Exception $e) {
        //handle error
    }

    if ($response && $response->isSuccess()) {
        $result = $response->getBody();            
    } else {
        $error = $response->getBody();
    }

you don't have to use the entire framework just include (or autoload) the classes that you need.

Exlord
  • 5,009
  • 4
  • 31
  • 51
  • can't use framework at this stage as it is too late. Thumbs upp for effort. just have to replace the httprequest with something else.. –  Apr 22 '14 at 07:19
  • as i said `you don't have to use the entire framework just include (or autoload) the classes that you need.` – Exlord Apr 22 '14 at 07:54
1

Use the cURL in php

<?php
// A very simple PHP example that sends a HTTP POST to a remote site
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://example.com/feed.rss");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"postvar1=value1&postvar2=value2");

// in real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS, 
//          http_build_query(array('postvar1' => 'value1')));

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);

curl_close ($ch);

// further processing ....
if ($server_output == "OK") { ... } else { ... }

?>

for more see this PHP Difference between Curl and HttpRequest

Community
  • 1
  • 1
sandip
  • 3,279
  • 5
  • 31
  • 54
  • thanks @sandip but when i used this on server its not working as response shows nothing in variable.that is empty –  Apr 22 '14 at 07:16
  • @X-Man yes, may be on your server the `cURl` is not enabled, you have to enable it see how to enable it : http://stackoverflow.com/questions/1347146/how-to-enable-curl-in-php-xampp.`it depends on which server you are using(XAMPP,WAMP,LAMP etc.)` – sandip Apr 22 '14 at 07:22
  • @X-Man if you are using the WAMP it is pretty easy, just `click on the icon of Wamp` on the right side of the `task-bar`, click on `extension` then look for `curl` click on it and `restart the WAMP` then run the script, it will work.. – sandip Apr 22 '14 at 07:52
  • mate its working on localhost, but not on hosting server as i've shared linux hosting –  Apr 22 '14 at 08:28
  • @X-Man ask your hosting provider to enable the `cURL`, to check whether it is already enabled or not, simple `echo phpinfo()` look for `cURL` in the output. – sandip Apr 22 '14 at 08:36
0

a slight variaton on the curl methods proposed, i decode the json that is returned, like in this snippet

Community
  • 1
  • 1
tony gil
  • 9,424
  • 6
  • 76
  • 100