Skip to content
This repository has been archived by the owner on Dec 22, 2020. It is now read-only.

Commit

Permalink
Release 0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
zQueal committed Mar 22, 2014
0 parents commit 2da83aa
Show file tree
Hide file tree
Showing 12 changed files with 1,201 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Zachary Queal

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.
211 changes: 211 additions & 0 deletions Network/Curl/Curl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
<?php

namespace Network\Curl;

/**
* Curl
*
* Simple cURL wrapper for basic HTTP requests. Support available
* for GET, POST, PUT, PATCH and DELETE
*
* @author dsyph3r <[email protected]>
*/
class Curl
{
/**
* Constants for available HTTP methods
*/
const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const PATCH = 'PATCH';
const DELETE = 'DELETE';

/**
* @var cURL handle
*/
private $curl;

/**
* Create the cURL resource
*/
public function __construct()
{
$this->curl = curl_init();
}

/**
* Clean up the cURL handle
*/
public function __destruct()
{
if (is_resource($this->curl))
{
curl_close($this->curl);
}
}

/**
* Get the cURL handle
*
* @return cURL cURL handle
*/
public function getCurl()
{
return $this->curl;
}

/**
* Make a HTTP GET request
*
* @param string $url Full URL including protocol
* @param array $params Any GET params
* @param array $options Additional options for the request
* @return array Response
*/
public function get($url, $params = array(), $options = array())
{
return $this->request($url, self::GET, $params, $options);
}

/**
* Make a HTTP POST request
*
* @param string $url Full URL including protocol
* @param array $params Any POST params
* @param array $options Additional options for the request
* @return array Response
*/
public function post($url, $params = array(), $options = array())
{
return $this->request($url, self::POST, $params, $options);
}

/**
* Make a HTTP PUT request
*
* @param string $url Full URL including protocol
* @param array $params Any PUT params
* @param array $options Additional options for the request
* @return array Response
*/
public function put($url, $params = array(), $options = array())
{
return $this->request($url, self::PUT, $params, $options);
}

/**
* Make a HTTP PATCH request
*
* @param string $url Full URL including protocol
* @param array $params Any PATCH params
* @param array $options Additional options for the request
* @return array Response
*/
public function patch($url, $params = array(), $options = array())
{
return $this->request($url, self::PATCH, $params, $options);
}

/**
* Make a HTTP DELETE request
*
* @param string $url Full URL including protocol
* @param array $params Any DELETE params
* @param array $options Additional options for the request
* @return array Response
*/
public function delete($url, $params = array(), $options = array())
{
return $this->request($url, self::DELETE, $params, $options);
}

/**
* Make a HTTP request
*
* @param string $url Full URL including protocol
* @param string $method HTTP method
* @param array $params Any params
* @param array $options Additional options for the request
* @return array Response
*/
protected function request($url, $method = self::GET, $params = array(), $options = array())
{
curl_setopt($this->curl, CURLOPT_HEADER, true);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);

curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($params));

// Check for custom headers
if (isset($options['headers']) && count($options['headers']))
curl_setopt($curl, CURLOPT_HTTPHEADER, $options['headers']);

// Check for basic auth
if (isset($options['auth']['type']) && "basic" === $options['auth']['type'])
curl_setopt($curl, CURLOPT_USERPWD, $options['auth']['username'] . ':' . $options['auth']['password']);

$response = $this->doCurl();

// Separate headers and body
$responseSplit = preg_split('/((?:\\r?\\n){2})/', $response['response']);
$responseCount = count($responseSplit);

$results = array(
'curl_info' => $response['curl_info'],
'status' => $response['curl_info']['http_code'],
'headers' => $this->splitHeaders($responseSplit[$responseCount-2]),
'data' => $responseSplit[$responseCount-1],
);

return $results;
}

/**
* Split the HTTP headers
*
* @param string $rawHeaders Raw HTTP headers
* @return array Key/Value headers
*/
protected function splitHeaders($rawHeaders)
{
$headers = array();

$headerLines = explode("\n", $rawHeaders);
$headers['HTTP'] = array_shift($headerLines);
foreach ($headerLines as $line) {
$header = explode(":", $line, 2);
$headers[trim($header[0])] = trim($header[1]);
}

return $headers;
}

/**
* Perform the Curl request
*
* @param cURL Handle $curl The cURL handle to use
* @return array cURL response
*/
protected function doCurl()
{
$response = curl_exec($this->curl);
$curlInfo = curl_getinfo($this->curl);

$results = array(
'curl_info' => $curlInfo,
'response' => $response,
);

return $results;
}

}

/**
* General Curl Exception
*/
class CurlException extends \Exception
{
}
120 changes: 120 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
About
==============

A simple configuration script to pull `city`, `state`, and `country` from a given zipcode. This is more of a proof of concept that even though this is now 2014 we're still requiring people to put in their full addresses when its totally unnecessary. The included class and concept can be used with webstores and the like to ensure that checking out for your customers is as simple as possible. The only requirements would be a PO BOX or street address and zipcode.

####Index
* [Namespace](#namespace)
* [About Phalcon](#about-phalcon)
* [Built In Webserver](#built-in-webserver)
* [Apache](#apache)
* [Nginx](#nginx)
* [Classes](#classes)
* [ZipCode](#zipcode)
* [Pull Requests and Support](#pulls-and-support)

Namespace
==============

Ensure you have at least PHP version `5.3.0` to be able to work with namespaces. The two that are used in this project are `Phalcon` and `Curl`.

```php
include 'Network/Curl/Curl.php';

use Phalcon\Mvc\Micro as Micro;
use Network\Curl\Curl as Curl;
```

Phalcon is a super fast C extension for PHP and is very easy to install and use. This example uses the Micro application class. `Network\Curl\Curl` is a very simplistic [cURL library](https://github.com/Xanza/curl-php) for creating simple cURL calls.

About Phalcon
==============

To be able to use phalcon it must be installed as a PHP extension and you must use the correct routing method for your preferred setup.

------------------------
####Built In Webserver

The built in webserver that we all have access to with the newer versions of PHP works just fine with the micro application functions included with Phalcon. However, to access them you must create another PHP file `.htrouter.php` and include it with your `index.php`.

```php
<?php

if(!file_exists(__DIR__ . '/' . $_SERVER['REQUEST_URI'])){
$_GET['_url'] = $_SERVER['REQUEST_URI'];
}

return false;
```

Once you've done that, it will route just fine.

------------------------
####Nginx

An example nginx `server{ }` function would look something like this:

```php
server {

listen 80;
server_name localhost.dev;

index index.php index.html index.htm;
set $root_path '/var/www/phalcon/public';
root $root_path;

try_files $uri $uri/ @rewrite;

location @rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}

location ~ \.php {
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index /index.php;

include /etc/nginx/fastcgi_params;

fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
}

location ~ /\.ht {
deny all;
}
}
```

------------------------
####Apache

Apache must simply have `AllowOverride All` enabled for your project directory, and include a `.htaccess` file with the following contents:

```php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
```

Classes
==============

There is only a single class in this example: `ZipCode`. It's a static method which is used to execute the cURL request and dump the results into an object (`$d`) and retuns the value.

--------------
####ZipCode

This static method class is used to facilitate the return of the different types of data that we can access, including `city`, `state`, and `country`. Each return has their own static function along with a static function for `all` which will return all three data values.


Pulls and Support
==================
I do not actively maintain this repository. If you have an improvement I'm open to pull requests, but I do not offer support.
17 changes: 17 additions & 0 deletions conf/release.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"non-interactive": false,
"dry-run": false,
"verbose": false,
"force": false,
"pkgFiles": ["package.json"],
"increment": "patch",
"commitMessage": "Release %s",
"tagName": "%s",
"tagAnnotation": "Release %s",
"buildCommand": false,
"distRepo": false,
"distStageDir": ".stage",
"distBase": "dist",
"distFiles": ["**/*"],
"publish": false
}
Loading

0 comments on commit 2da83aa

Please sign in to comment.