Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
Wpct Http Bridge
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
codeccoop
WordPress
plugins
Wpct Http Bridge
Commits
a5fc5767
Commit
a5fc5767
authored
1 year ago
by
Lucas García
Browse files
Options
Downloads
Patches
Plain Diff
feat: post multipart
parent
b476143f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
includes/class-http-client.php
+62
-0
62 additions, 0 deletions
includes/class-http-client.php
includes/class-multipart.php
+68
-0
68 additions, 0 deletions
includes/class-multipart.php
with
130 additions
and
0 deletions
includes/class-http-client.php
+
62
−
0
View file @
a5fc5767
...
...
@@ -2,6 +2,8 @@
namespace
WPCT_HB
;
require_once
'class-multipart.php'
;
class
Http_Client
{
public
static
function
get
(
$url
,
$headers
=
[])
...
...
@@ -29,6 +31,31 @@ class Http_Client
return
Http_Client
::
do_request
(
$url
,
$args
);
}
public
static
function
post_multipart
(
$url
,
$data
=
[],
$files
=
[],
$headers
=
[])
{
$url
=
Http_Client
::
get_endpoint_url
(
$url
);
$multipart
=
new
Multipart
();
$multipart
->
add_array
(
$data
);
foreach
(
$files
as
$name
=>
$path
)
{
$filename
=
basename
(
$path
);
$filetype
=
wp_check_filetype
(
$filename
);
if
(
!
$filetype
[
'type'
])
{
$filetype
[
'type'
]
=
mime_content_type
(
$path
);
}
$multipart
->
add_file
(
$name
,
$path
,
$filetype
[
'type'
]);
}
$headers
=
Http_Client
::
req_headers
(
$headers
,
'POST'
,
$url
);
$headers
[
'Content-Type'
]
=
$multipart
->
content_type
();
$args
=
[
'method'
=>
'POST'
,
'headers'
=>
$headers
,
'body'
=>
$multipart
->
data
()
];
return
Http_Client
::
do_request
(
$url
,
$args
);
}
public
static
function
put
(
$url
,
$data
=
[],
$headers
=
[])
{
$url
=
Http_Client
::
get_endpoint_url
(
$url
);
...
...
@@ -44,6 +71,31 @@ class Http_Client
return
Http_Client
::
do_request
(
$url
,
$args
);
}
public
static
function
put_multipart
(
$url
,
$data
=
[],
$files
=
[],
$headers
=
[])
{
$url
=
Http_Client
::
get_endpoint_url
(
$url
);
$multipart
=
new
Multipart
();
$multipart
->
add_array
(
$data
);
foreach
(
$files
as
$name
=>
$path
)
{
$filename
=
basename
(
$path
);
$filetype
=
wp_check_filetype
(
$filename
);
if
(
!
$filetype
[
'type'
])
{
$filetype
[
'type'
]
=
mime_content_type
(
$path
);
}
$multipart
->
add_file
(
$name
,
$path
,
$filetype
[
'type'
]);
}
$headers
=
Http_Client
::
req_headers
(
$headers
,
'PUT'
,
$url
);
$headers
[
'Content-Type'
]
=
$multipart
->
content_type
();
$args
=
[
'method'
=>
'PUT'
,
'headers'
=>
$headers
,
'body'
=>
$multipart
->
data
()
];
return
Http_Client
::
do_request
(
$url
,
$args
);
}
public
static
function
delete
(
$url
,
$headers
=
[])
{
$url
=
Http_Client
::
get_endpoint_url
(
$url
);
...
...
@@ -114,12 +166,22 @@ function wpct_hb_post($url, $data = [], $headers = [])
return
Http_Client
::
post
(
$url
,
$data
,
$headers
);
}
function
wpct_hn_post_multipart
(
$url
,
$data
=
[],
$files
=
[],
$headers
=
[])
{
return
Http_Client
::
post_multipart
(
$url
,
$data
,
$files
,
$headers
);
}
// Performs a put request to Odoo
function
wpct_hb_put
(
$url
,
$data
=
[],
$headers
=
[])
{
return
Http_Client
::
put
(
$url
,
$data
,
$headers
);
}
function
wpct_hb_put_multipart
(
$url
,
$data
=
[],
$files
=
[],
$headers
=
[])
{
return
Http_Client
::
put_multipart
(
$url
,
$data
,
$files
,
$headers
);
}
// Performs a delete request to Odoo
function
wpct_hb_delete
(
$url
,
$headers
=
[])
{
...
...
This diff is collapsed.
Click to expand it.
includes/class-multipart.php
0 → 100644
+
68
−
0
View file @
a5fc5767
<?php
namespace
WPCT_HB
;
class
Multipart
{
const
EOL
=
"
\r\n
"
;
private
$_data
=
''
;
private
$_mime_boundary
;
public
function
__construct
()
{
$this
->
_mime_boundary
=
md5
(
microtime
(
true
));
}
private
function
_add_part_header
()
{
$this
->
_data
.
=
'--'
.
$this
->
_mime_boundary
.
self
::
EOL
;
}
public
function
add_array
(
$data
,
$prefix
=
''
)
{
foreach
(
$data
as
$key
=>
$value
)
{
if
(
is_array
(
$value
))
{
if
(
$prefix
)
$this
->
add_array
(
$value
,
$prefix
.
'['
.
$key
.
']'
);
else
$this
->
add_array
(
$value
,
$key
);
}
else
{
if
(
$prefix
)
$this
->
add_part
(
$prefix
.
'['
.
(
is_numeric
(
$key
)
?
''
:
$key
)
.
']'
,
$value
);
else
$this
->
add_part
(
$key
,
$value
);
}
}
}
public
function
add_part
(
$key
,
$value
)
{
$this
->
_add_part_header
();
$this
->
_data
.
=
'Content-Disposition: form-data; name="'
.
$key
.
'"'
.
self
::
EOL
;
$this
->
_data
.
=
self
::
EOL
;
$this
->
_data
.
=
$value
.
self
::
EOL
;
}
public
function
add_file
(
$key
,
$filename
,
$type
,
$content
=
null
)
{
$this
->
_add_part_header
();
$this
->
_data
.
=
'Content-Disposition: form-data; name="'
.
$key
.
'"; filename="'
.
basename
(
$filename
)
.
'"'
.
self
::
EOL
;
$this
->
_data
.
=
'Content-Type: '
.
$type
.
self
::
EOL
;
$this
->
_data
.
=
'Content-Transfer-Encoding: binary'
.
self
::
EOL
;
$this
->
_data
.
=
self
::
EOL
;
if
(
!
$content
)
$this
->
_data
.
=
file_get_contents
(
$filename
)
.
self
::
EOL
;
else
$this
->
_data
.
=
$content
.
self
::
EOL
;
}
public
function
content_type
()
{
return
'multipart/form-data; boundary='
.
$this
->
_mime_boundary
;
}
public
function
data
()
{
// add the final content boundary
return
$this
->
_data
.
=
'--'
.
$this
->
_mime_boundary
.
'--'
.
self
::
EOL
.
self
::
EOL
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment