You are hereHttp POST using HttpFul PHP REST library

Http POST using HttpFul PHP REST library



By admin - Posted on 31 October 2017

Spent considerable amount of time to find a working solution to do http post with httpful php library, even though spent some time to find a solution from internet & httpful documentation, unfortunately couldn't find exact http post usage with httpful.

The problem faced was that whenever http post is tried, instead of going as post parameters, the parameters were sent as part of http request body.

The solution was to use code "->body($arr, Httpful\Mime::FORM)" rather than the logical "->body($arr)"

Sharing following working code to make things easier for php community. In case if have trouble to debug, you may use excellent online http request debug service from requestb.in website

require 'httpful.phar'; //using phar library makes things easier

public function SendMessage()
{
$url='http://sampleUrl.com/postTest.php'; //post url here
$arr=array('user-name'=>'name','password'=>'pass','message'=>'msg'); //post params here
$resp = \Httpful\Request::post($url) // Build a Post request...
->body($arr, Httpful\Mime::FORM) // Httpful\Mime::FORM => causes to do http post
->expectsJson()
->send();
//return json_encode($resp->body); //if ->expectsJson() line not there
return ($resp->body);
}

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
7 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

Subscribe to Getzy by e-mail

Delivered by FeedBurner

Syndicate

Syndicate content