Answers
try {
$url = 'https://httpbin.org/post1'; // hasErrors
// $url = 'https://httpbin.org1/post'; // ConnectionErrorException
$msg = 'qwe';
$req = \Httpful\Request::post($url)
->addHeaders([
'Content-Type' => 'application/x-www-form-urlencoded',
])
->body('mystring=' . urlencode($msg));
$res = $req->send();
if ($res->hasErrors()) {
print_r("
<pre>
*** REQUEST ***
Headers: {$req->raw_headers}
Body: {$req->payload}
*** RESPONSE ***
Headers: {$res->raw_headers}
Body: <xmp>{$res->raw_body}</xmp>
</pre>
");
}
} catch (\Httpful\Exception\ConnectionErrorException $e) {
if (isset($req)) {
print_r("
<pre>
*** REQUEST ***
Headers: {$req->raw_headers}
Body: {$req->payload}
</pre>
");
}
if (isset($res)) {
print_r("
<pre>
*** RESPONSE ***
Headers: {$res->raw_headers}
Body: <xmp>{$res->raw_body}</xmp>
</pre>
");
}
}
0 comments:
Post a Comment
Thanks