Skip to content

Commit

Permalink
fixed #1 Unable to post json data
Browse files Browse the repository at this point in the history
  • Loading branch information
Joungkyun committed Jun 14, 2022
1 parent 8ef7d45 commit 2160f5c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
25 changes: 18 additions & 7 deletions HTTPRelay.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ function __construct ($header = null) {
if ( $header['POSTTYPE'] ) {
switch ($header['POSTTYPE']) {
case 'form-data' :
case 'json' :
case 'url-encode' :
$this->posttype = $header['POSTTYPE'];
}
Expand Down Expand Up @@ -275,9 +276,6 @@ public function fetch ($to, $tmout = 60, $httphost = '', $post = null, $recursio
# header information
$header = self::http_header ();

if ( $this->debug )
fprintf (STDERR, "** Header SET :\n%s\n", print_r ($header, true));

curl_setopt ($c, CURLOPT_URL, $to);
curl_setopt ($c, CURLOPT_TIMEOUT, $tmout);
curl_setopt ($c, CURLOPT_NOPROGRESS, 1);
Expand All @@ -288,19 +286,32 @@ public function fetch ($to, $tmout = 60, $httphost = '', $post = null, $recursio
);
curl_setopt ($c, CURLOPT_HEADER, 0);
curl_setopt ($c, CURLOPT_NOBODY, 0);
curl_setopt ($c, CURLOPT_HTTPHEADER, $header);
curl_setopt ($c, CURLOPT_FAILONERROR, 1);
curl_setopt ($c, CURLINFO_HEADER_OUT, 1);
if ( preg_match ('/^https:/', $to) )
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);

if ( $post && is_array ($post) ) {
if ( $post ) {
curl_setopt ($c, CURLOPT_POST, 1);
if ( $this->posttype == 'url-encode' )
$post = http_build_query ($post);
if ( is_array ($post) ) {
if ( $this->posttype == 'json' ) {
self::set_header ($header, 'Content-Type', 'application/json');
$post = json_encode ($post);
} else if ( $this->posttype == 'url-encode' )
$post = http_build_query ($post);
} else {
// strings are json format, change Content-Type to 'application/json'
if ( json_decode ($post) != NULL )
self::set_header ($header, 'Content-Type', 'application/json');
}
curl_setopt ($c, CURLOPT_POSTFIELDS, $post);
}

curl_setopt ($c, CURLOPT_HTTPHEADER, $header);

if ( $this->debug )
fprintf (STDERR, "** Header SET :\n%s\n", print_r ($header, true));

$data = curl_exec ($c);
$info = $this->set_return_info ($c);

Expand Down
10 changes: 6 additions & 4 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<active>yes</active>
</lead>
<date>2022-06-14</date>
<time>21:23:33</time>
<time>21:56:27</time>
<version>
<release>1.0.6</release>
<api>1.0.0</api>
Expand All @@ -22,11 +22,12 @@
<api>stable</api>
</stability>
<license>BSD</license>
<notes>* fixed #2 Overwrite for existing headers in set_header method
<notes>* fixed #1 Unable to post json data
* fixed #2 Overwrite for existing headers in set_header method
</notes>
<contents>
<dir name="/">
<file role="php" md5sum="1e99dce2b9183c4e5b85538e56fc769b" name="HTTPRelay.php" />
<file role="php" md5sum="04ca4e0b2b6a8e9de67ed691f4f16c55" name="HTTPRelay.php" />
</dir>
</contents>
<dependencies>
Expand Down Expand Up @@ -57,7 +58,8 @@
</stability>
<date>2022-06-14</date>
<license>BSD</license>
<notes>* fixed #2 Overwrite for existing headers in set_header method
<notes>* fixed #1 Unable to post json data
* fixed #2 Overwrite for existing headers in set_header method
</notes>
</release>
<release>
Expand Down
6 changes: 4 additions & 2 deletions package.xml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
<api>stable</api>
</stability>
<license>BSD</license>
<notes>* fixed #2 Overwrite for existing headers in set_header method
<notes>* fixed #1 Unable to post json data
* fixed #2 Overwrite for existing headers in set_header method
</notes>
<contents>
<dir name="/">
Expand Down Expand Up @@ -57,7 +58,8 @@
</stability>
<date>@curdate@</date>
<license>BSD</license>
<notes>* fixed #2 Overwrite for existing headers in set_header method
<notes>* fixed #1 Unable to post json data
* fixed #2 Overwrite for existing headers in set_header method
</notes>
</release>
<release>
Expand Down

0 comments on commit 2160f5c

Please sign in to comment.