Skip to content

Commit 9e0ea15

Browse files
author
Josh Pettett
committed
support sending in-memory string as file content
1 parent 1e51025 commit 9e0ea15

3 files changed

Lines changed: 43 additions & 2 deletions

File tree

examples/sendText.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
require_once('phaxio_config.php');
4+
require_once('autoload.php');
5+
6+
$phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
7+
8+
$params = array(
9+
'to' => $toNumber,
10+
'file' => array(
11+
$phaxio->stringUpload('Page 1'),
12+
$phaxio->stringUpload('<b>Page 2<b>')
13+
)
14+
);
15+
16+
$result = $phaxio->sendFax($params);
17+
var_dump($result);
18+

lib/Phaxio.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public function retrieveDefaultPhaxCode($getMetadata = false)
6363
return Phaxio\PhaxCode::init($this)->retrieve($getMetadata);
6464
}
6565

66+
public function stringUpload($str, $ext = 'txt') {
67+
return new Phaxio\StringUpload($str, $ext);
68+
}
69+
6670
# API client methods
6771

6872
public function getApiKey()
@@ -186,13 +190,18 @@ private function curlSetoptCustomPostfields($ch, $postfields, $headers = null)
186190
}
187191
foreach ($fields as $field) {
188192
list($key, $value) = $field;
193+
189194
if (is_resource($value)) {
195+
$value = $this->stringUpload(stream_get_contents($value), basename(stream_get_meta_data($value)['uri']));
196+
}
197+
198+
if ($value instanceof Phaxio\StringUpload) {
190199
$filename = stream_get_meta_data($value)['uri'];
191200
$body[] = '--' . $boundary;
192-
$body[] = 'Content-Disposition: form-data; name="' . $key . '"; filename="' . basename($filename) . '"';
201+
$body[] = 'Content-Disposition: form-data; name="' . $key . '"; filename="string.' . $value->extension . '"';
193202
$body[] = 'Content-Type: application/octet-stream';
194203
$body[] = '';
195-
$body[] = stream_get_contents($value);
204+
$body[] = $value->string;
196205
} else {
197206
$body[] = '--' . $boundary;
198207
$body[] = 'Content-Disposition: form-data; name="' . $key . '"';

lib/Phaxio/StringUpload.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Phaxio;
4+
5+
class StringUpload
6+
{
7+
public $string;
8+
public $extension;
9+
10+
public function __construct($str, $ext) {
11+
$this->string = $str;
12+
$this->extension = $ext;
13+
}
14+
}

0 commit comments

Comments
 (0)