PHP code example of nidux / niduxrest-php
1. Go to this page and download the library: Download nidux/niduxrest-php library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nidux / niduxrest-php example snippets
$headers = ['Accept' => 'application/json'];
$query = ['foo' => 'hello', 'bar' => 'world'];
$response = Niduxrest\Request::post('http://mockbin.com/request', $headers, $query);
$response->getCode(); // HTTP Status code via getter
$response->getHeaders(); // Headers via getter
$response->getBody(); // Parsed body via getter
$response->getRawBody(); // Unparsed body via getter
$headers = ['Accept' => 'application/json'];
$data = ['name' => 'ahmad', 'company' => 'mashape'];
$body = Niduxrest\Request\Body::prepareJson($data);
$response = Niduxrest\Request::post('http://mockbin.com/request', $headers, $body);
$headers = ['Accept' => 'application/json'];
$data = ['name' => 'ahmad', 'company' => 'mashape'];
$body = Niduxrest\Request\Body::prepareForm($data);
$response = Niduxrest\Request::post('http://mockbin.com/request', $headers, $body);
$headers = ['Accept' => 'application/json'];
$data = ['name' => 'ahmad', 'company' => 'mashape'];
$body = Niduxrest\Request\Body::prepareMultiPart($data);
$response = Niduxrest\Request::post('http://mockbin.com/request', $headers, $body);
$headers = ['Accept' => 'application/json'];
$data = ['name' => 'ahmad', 'company' => 'mashape'];
$files = ['bio' => '/path/to/bio.txt', 'avatar' => '/path/to/avatar.jpg'];
$body = Niduxrest\Request\Body::prepareMultiPart($data, $files);
$response = Niduxrest\Request::post('http://mockbin.com/request', $headers, $body);
$headers = ['Accept' => 'application/json'];
$body = [
'name' => 'ahmad',
'company' => 'mashape'
'bio' => Niduxrest\Request\Body::prepareFile('/path/to/bio.txt', 'text/plain'),
'avatar' => Niduxrest\Request\Body::prepareFile('/path/to/my_avatar.jpg', 'text/plain', 'avatar.jpg')
];
$response = Niduxrest\Request::post('http://mockbin.com/request', $headers, $body);
$headers = ['Accept' => 'application/json', 'Content-Type' => 'application/x-php-serialized'];
$body = serialize((['foo' => 'hello', 'bar' => 'world']);
$response = Niduxrest\Request::post('http://mockbin.com/request', $headers, $body);
// auth with bearer token
Niduxrest\Request::setBearerToken('exampleOfSuperSecretBearerToken');
// basic auth
Niduxrest\Request::setAuthenticationMethod('username', 'password');
// Mashape auth
Niduxrest\Request::setMashapeKey('<mashape_key>');
// custom auth method
Niduxrest\Request::setProxyAuthentication('username', 'password', CURLAUTH_DIGEST);
$response = Niduxrest\Request::get('http://mockbin.com/request', null, null, 'username', 'password');
Niduxrest\Request::setCookie($cookie)
Niduxrest\Request::setCookieFile($cookieFile)
Niduxrest\Request::get($url, $headers = [], $parameters = null)
Niduxrest\Request::post($url, $headers = [], $body = null)
Niduxrest\Request::put($url, $headers = [], $body = null)
Niduxrest\Request::patch($url, $headers = [], $body = null)
Niduxrest\Request::delete($url, $headers = [], $body = null)
Niduxrest\Request::send(Niduxrest\Enum\Method::LINK, $url, $headers = [], $body);
Niduxrest\Request::send(Niduxrest\Enum\Method::CHECKOUT, $url, $headers = [], $body);
Niduxrest\Request::send(Niduxrest\Enum\Method::HEAD, $url, $headers = [], $body);
Niduxrest\Request::send(Niduxrest\Enum\Method::LOCK, $url, $headers = [], $body);
Niduxrest\Request::setJsonOpts(true, 512, JSON_NUMERIC_CHECK & JSON_FORCE_OBJECT & JSON_UNESCAPED_SLASHES);
Niduxrest\Request::setTimeout(5); // 5s timeout
// quick setup with default port: 1080
Niduxrest\Request::setProxy('10.10.10.1');
// custom port and proxy type
Niduxrest\Request::setProxy('10.10.10.1', 8080, CURLPROXY_HTTP);
// enable tunneling
Niduxrest\Request::setProxy('10.10.10.1', 8080, CURLPROXY_HTTP, true);
// basic auth
Niduxrest\Request::setProxyAuthentication('username', 'password');
// basic auth
Niduxrest\Request::setProxyAuthentication('username', 'password', CURLAUTH_DIGEST);
Niduxrest\Request::setIndidualDefaultHeader('Header1', 'Value1');
Niduxrest\Request::setIndidualDefaultHeader('Header2', 'Value2');
Niduxrest\Request::setDefaultHeaders([
'Header1' => 'Value1',
'Header2' => 'Value2'
]);
Niduxrest\Request::clearDefaultHeaders();
Niduxrest\Request::setIndividualCurlOpt(CURLOPT_COOKIE, 'foo=bar');
Niduxrest\Request::setCurlOpts([
CURLOPT_COOKIE => 'foo=bar'
]);
Niduxrest\Request::clearCurlOpts();
Niduxrest\Request::setVerifyPeer(false); // Disables SSL cert validation
// alias for `curl_getinfo`
Niduxrest\Request::getInfo()
// returns internal cURL handle
Niduxrest\Request::getCurlHandle()
json
{
"ux/niduxrest-php": "2.*"
}
}
shell
composer