Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

Commit

Permalink
Post data support
Browse files Browse the repository at this point in the history
  • Loading branch information
sankarsuda committed May 8, 2018
1 parent 3bd7f45 commit 7ea43eb
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Changelog

All notable changes to `mobtexting` will be documented in this file
55 changes: 55 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Contributing

Contributions are **welcome** and will be fully **credited**.

Please read and understand the contribution guide before creating an issue or pull request.

## Etiquette

This project is open source, and as such, the maintainers give their free time to build and maintain the source code
held within. They make the code freely available in the hope that it will be of use to other developers. It would be
extremely unfair for them to suffer abuse or anger for their hard work.

Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the
world that developers are civilized and selfless people.

It's the duty of the maintainer to ensure that all submissions to the project are of sufficient
quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used.

## Viability

When requesting or submitting new features, first consider whether it might be useful to others. Open
source projects are used by many developers, who may have entirely different needs to your own. Think about
whether or not your feature is likely to be used by other users of the project.

## Procedure

Before filing an issue:

- Attempt to replicate the problem, to ensure that it wasn't a coincidental incident.
- Check to make sure your feature suggestion isn't already present within the project.
- Check the pull requests tab to ensure that the bug doesn't have a fix in progress.
- Check the pull requests tab to ensure that the feature isn't already in progress.

Before submitting a pull request:

- Check the codebase to ensure that your feature doesn't already exist.
- Check the pull requests to ensure that another person hasn't already submitted the feature or fix.

## Requirements

If the project maintainer has any additional requirements, you will find them listed here.

- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer).

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.

**Happy coding**!
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# The MIT License (MIT)

Copyright (c) Gregorio Hernández Caso <[email protected]>

> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in
> all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> THE SOFTWARE.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@ composer require mobtexting/php-http-client
## Usage

```php
$headers = ['Authorization: Bearer ' . $apiKey];
$token = 'xxxx';

$headers = ['Authorization: Bearer ' . $token];
$client = new Mobtexting\Client('https://api.example.com', $headers);

$data = [
'some' => 1, 'awesome' => 2, 'data' => 3
];

$queryParams = [
'hello' => 0, 'world' => 1
];

$requestHeaders = [
'X-Test' => 'test'
];

$response = $client->post($data, $queryParams, $requestHeaders);

var_dump(
Expand Down
28 changes: 19 additions & 9 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* @method Response put($body = null, $query = null, $headers = null)
* @method Response delete($body = null, $query = null, $headers = null)
*
* @method Client version($value)
* @method Client|Response send()
*
*
Expand All @@ -29,14 +28,11 @@ class Client
* @var array
*/
protected $headers;
/**
* @var string
*/
protected $version;

/**
* @var array
*/
protected $path;
protected $path = [];
/**
* @var array
*/
Expand Down Expand Up @@ -172,10 +168,11 @@ public function setIsConcurrentRequest($isConcurrent)
*/
protected function buildUrl($queryParams = null)
{
$path = '/' . implode('/', $this->path);
$path = '/' . (is_array($this->path) ? implode('/', $this->path) : '');
if (isset($queryParams)) {
$path .= '?' . http_build_query($queryParams);
}

return sprintf('%s%s', $this->host, $path);
}
/**
Expand All @@ -197,16 +194,21 @@ protected function createCurlOptions($method, $body = null, $headers = null)
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_FAILONERROR => false,
] + $this->curlOptions;

if (isset($headers)) {
$headers = array_merge($this->headers, $headers);
} else {
$headers = $this->headers;
}
if (isset($body)) {

if (isset($body) && is_array($body)) {
$encodedBody = json_encode($body);
$options[CURLOPT_POSTFIELDS] = $encodedBody;
$headers = array_merge($headers, ['Content-Type: application/json']);
} else if (isset($body)) {
$options[CURLOPT_POSTFIELDS] = $body;
}

$options[CURLOPT_HTTPHEADER] = $headers;

return $options;
Expand Down Expand Up @@ -291,6 +293,7 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
{
$channel = curl_init($url);
$options = $this->createCurlOptions($method, $body, $headers);

curl_setopt_array($channel, $options);
$content = curl_exec($channel);
$response = $this->parseResponse($channel, $content);
Expand Down Expand Up @@ -370,6 +373,7 @@ public function _($name = null)

return $client;
}

/**
* Dynamically add method calls to the url, then call a method.
* (e.g. client.name.name.method())
Expand All @@ -390,9 +394,15 @@ public function __call($name, $args)
if (in_array($name, $this->methods, true)) {
$body = isset($args[0]) ? $args[0] : null;
$queryParams = isset($args[1]) ? $args[1] : null;
$url = $this->buildUrl($queryParams);
$headers = isset($args[2]) ? $args[2] : null;
$retryOnLimit = isset($args[3]) ? $args[3] : $this->retryOnLimit;

if ($body && strtolower($name) == 'post') {
$body = http_build_query($body);
}

$url = $this->buildUrl($queryParams);

if ($this->isConcurrentRequest) {
// save request to be sent later
$requestData = ['method' => $name, 'url' => $url, 'body' => $body, 'headers' => $headers];
Expand Down

0 comments on commit 7ea43eb

Please sign in to comment.