mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-08-30 12:14:36 +00:00
Merge pull request #540 from zachborboa/master
Allow objects implementing JsonSerializable to be serialized
This commit is contained in:
+31
-24
@@ -139,32 +139,39 @@ class Curl
|
||||
public function buildPostData($data)
|
||||
{
|
||||
$binary_data = false;
|
||||
if (is_array($data)) {
|
||||
// Return JSON-encoded string when the request's content-type is JSON.
|
||||
if (isset($this->headers['Content-Type']) &&
|
||||
preg_match($this->jsonPattern, $this->headers['Content-Type'])) {
|
||||
$data = \Curl\json_encode($data);
|
||||
} else {
|
||||
// Manually build a single-dimensional array from a multi-dimensional array as using curl_setopt($ch,
|
||||
// CURLOPT_POSTFIELDS, $data) doesn't correctly handle multi-dimensional arrays when files are
|
||||
// referenced.
|
||||
if (ArrayUtil::is_array_multidim($data)) {
|
||||
$data = ArrayUtil::array_flatten_multidim($data);
|
||||
}
|
||||
|
||||
// Modify array values to ensure any referenced files are properly handled depending on the support of
|
||||
// the @filename API or CURLFile usage. This also fixes the warning "curl_setopt(): The usage of the
|
||||
// @filename API for file uploading is deprecated. Please use the CURLFile class instead". Ignore
|
||||
// non-file values prefixed with the @ character.
|
||||
foreach ($data as $key => $value) {
|
||||
if (is_string($value) && strpos($value, '@') === 0 && is_file(substr($value, 1))) {
|
||||
$binary_data = true;
|
||||
if (class_exists('CURLFile')) {
|
||||
$data[$key] = new \CURLFile(substr($value, 1));
|
||||
}
|
||||
} elseif ($value instanceof \CURLFile) {
|
||||
$binary_data = true;
|
||||
// Return JSON-encoded string when the request's content-type is JSON and the data is serializable.
|
||||
if (isset($this->headers['Content-Type']) &&
|
||||
preg_match($this->jsonPattern, $this->headers['Content-Type']) &&
|
||||
(
|
||||
is_array($data) ||
|
||||
(
|
||||
is_object($data) &&
|
||||
interface_exists('JsonSerializable', false) &&
|
||||
$data instanceof \JsonSerializable
|
||||
)
|
||||
)) {
|
||||
$data = \Curl\json_encode($data);
|
||||
} elseif (is_array($data)) {
|
||||
// Manually build a single-dimensional array from a multi-dimensional array as using curl_setopt($ch,
|
||||
// CURLOPT_POSTFIELDS, $data) doesn't correctly handle multi-dimensional arrays when files are
|
||||
// referenced.
|
||||
if (ArrayUtil::is_array_multidim($data)) {
|
||||
$data = ArrayUtil::array_flatten_multidim($data);
|
||||
}
|
||||
|
||||
// Modify array values to ensure any referenced files are properly handled depending on the support of
|
||||
// the @filename API or CURLFile usage. This also fixes the warning "curl_setopt(): The usage of the
|
||||
// @filename API for file uploading is deprecated. Please use the CURLFile class instead". Ignore
|
||||
// non-file values prefixed with the @ character.
|
||||
foreach ($data as $key => $value) {
|
||||
if (is_string($value) && strpos($value, '@') === 0 && is_file(substr($value, 1))) {
|
||||
$binary_data = true;
|
||||
if (class_exists('CURLFile')) {
|
||||
$data[$key] = new \CURLFile(substr($value, 1));
|
||||
}
|
||||
} elseif ($value instanceof \CURLFile) {
|
||||
$binary_data = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Helper;
|
||||
|
||||
use Curl\Curl;
|
||||
use \Curl\Curl;
|
||||
|
||||
class Test
|
||||
{
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace CurlTest;
|
||||
use \Curl\CaseInsensitiveArray;
|
||||
use \Curl\Curl;
|
||||
use \Helper\Test;
|
||||
use \Helper\User;
|
||||
|
||||
class CurlTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
@@ -3715,4 +3716,20 @@ class CurlTest extends \PHPUnit\Framework\TestCase
|
||||
$curl->unsetProxy();
|
||||
$this->assertNull($curl->getOpt(CURLOPT_PROXY));
|
||||
}
|
||||
|
||||
public function testJsonSerializable()
|
||||
{
|
||||
if (!interface_exists('JsonSerializable')) {
|
||||
$this->markTestSkipped();
|
||||
}
|
||||
|
||||
$expected_response = '{"name":"Alice","email":"alice@example.com"}';
|
||||
|
||||
$user = new \Helper\User('Alice', 'alice@example.com');
|
||||
$this->assertEquals($expected_response, json_encode($user));
|
||||
|
||||
$test = new Test();
|
||||
$test->curl->setHeader('Content-Type', 'application/json');
|
||||
$this->assertEquals($expected_response, $test->server('post_json', 'POST', $user));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Helper;
|
||||
|
||||
// Check interface exists to fix "Fatal error: Interface 'JsonSerializable' not found in ../tests/PHPCurlClass/User.php
|
||||
// on line X".
|
||||
if (interface_exists('JsonSerializable')) {
|
||||
class User implements \JsonSerializable
|
||||
{
|
||||
private $name;
|
||||
private $email;
|
||||
|
||||
public function __construct($name = null, $email = null)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->email = $email;
|
||||
}
|
||||
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return array(
|
||||
'name' => $this->name,
|
||||
'email' => $this->email,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user