|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Priceless PHP |
| 4 | + * e-mail API Client |
| 5 | + * |
| 6 | + * @author MarQuis Knox <[email protected]> |
| 7 | + * @copyright 2015 MarQuis Knox |
| 8 | + * @link http://marquisknox.com |
| 9 | + * @license GNU Affero General Public License v3 |
| 10 | + * |
| 11 | + * @since Tuesday, August 18, 2015, 17:30 GMT+1 |
| 12 | + * @modified $Date$ $Author$ |
| 13 | + * @version $Id$ |
| 14 | + * |
| 15 | + * @category API |
| 16 | + * @package Priceless PHP |
| 17 | +*/ |
| 18 | + |
| 19 | +error_reporting( E_ALL ); |
| 20 | +ini_set( 'display_errors', true ); |
| 21 | + |
| 22 | +$url = 'http://domain.tld/api.php'; |
| 23 | +$data = array( |
| 24 | + 'apiKey' => null, |
| 25 | + 'mailFrom' => null, |
| 26 | + 'mailReplyTo' => null, |
| 27 | + 'mailSubject' => '[TEST] API Test', |
| 28 | + 'mailText' => 'Testing...', |
| 29 | + 'mailTo' => null, |
| 30 | +); |
| 31 | + |
| 32 | +$result = curl_post_url( |
| 33 | + $url, |
| 34 | + $data |
| 35 | +); |
| 36 | + |
| 37 | +echo "Result: ".$result."\n"; |
| 38 | + |
| 39 | +/** |
| 40 | + * Post to a URL via cURL |
| 41 | + * |
| 42 | + * @link http://bavotasan.com/2011/post-url-using-curl-php |
| 43 | + * @link http://davidwalsh.name/curl-post |
| 44 | + * |
| 45 | + * @param string $url |
| 46 | + * @param array $data |
| 47 | + * @param array $headers |
| 48 | + * @param boolean $returnCurlInfo |
| 49 | + * @param int $timeout |
| 50 | + * @param int $maxRedirs |
| 51 | + * @return array |
| 52 | +*/ |
| 53 | +function curl_post_url( $url, $data = array(), $headers = array(), $returnCurlInfo = false, $timeout = 60, $maxRedirs = 10 ) |
| 54 | +{ |
| 55 | + if( empty( $data ) ) { |
| 56 | + return array(); |
| 57 | + } |
| 58 | + |
| 59 | + $fields = ''; |
| 60 | + foreach( $data AS $key => $value ) { |
| 61 | + // decode if already URL encoded, |
| 62 | + // this insures that the URL is URL encoded |
| 63 | + $fields .= $key . '=' . urlencode( urldecode( $value ) ) . '&'; |
| 64 | + } |
| 65 | + |
| 66 | + rtrim( $fields, '&' ); |
| 67 | + |
| 68 | + $post = curl_init(); |
| 69 | + |
| 70 | + if( !empty( $headers ) ) { |
| 71 | + curl_setopt( $post, CURLOPT_HTTPHEADER, $headers ); |
| 72 | + } |
| 73 | + curl_setopt( $post, CURLOPT_USERAGENT, 'PHP '. phpversion() ); |
| 74 | + curl_setopt( $post, CURLOPT_URL, $url ); |
| 75 | + curl_setopt( $post, CURLOPT_POST, count( $data ) ); |
| 76 | + curl_setopt( $post, CURLOPT_POSTFIELDS, $fields ); |
| 77 | + curl_setopt( $post, CURLOPT_FOLLOWLOCATION, true ); |
| 78 | + curl_setopt( $post, CURLOPT_MAXREDIRS, $maxRedirs ); |
| 79 | + curl_setopt( $post, CURLOPT_RETURNTRANSFER, true ); |
| 80 | + curl_setopt( $post, CURLOPT_BINARYTRANSFER, true ); |
| 81 | + curl_setopt( $post, CURLOPT_HEADER, false ); |
| 82 | + |
| 83 | + if( $returnCurlInfo ) { |
| 84 | + curl_setopt( $post, CURLINFO_HEADER_OUT, true ); |
| 85 | + } |
| 86 | + |
| 87 | + curl_setopt( $post, CURLOPT_SSL_VERIFYPEER, false ); |
| 88 | + curl_setopt( $post, CURLOPT_TIMEOUT, $timeout ); |
| 89 | + |
| 90 | + $response = curl_exec( $post ); |
| 91 | + |
| 92 | + if( $returnCurlInfo ) { |
| 93 | + $originalResponse = $response; |
| 94 | + $response = array(); |
| 95 | + $response['info'] = curl_getinfo( $post ); |
| 96 | + $response['html'] = $originalResponse; |
| 97 | + } |
| 98 | + |
| 99 | + if( $error = curl_error( $post ) ) { |
| 100 | + $response['error'] = $error; |
| 101 | + $response['errorno'] = curl_errno( $post ); |
| 102 | + } |
| 103 | + |
| 104 | + curl_close( $post ); |
| 105 | + |
| 106 | + return $response; |
| 107 | +} |
0 commit comments