Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adicahyaludin/a46ec2830b397f67db30a489f73ffe72 to your computer and use it in GitHub Desktop.
Save adicahyaludin/a46ec2830b397f67db30a489f73ffe72 to your computer and use it in GitHub Desktop.
CURL PHP Handle File Upload via AJAX JS
$data = array(
'email' => $_POST['email'],
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'gender' => $_POST['gender'],
'pass1' => $_POST['pass1'],
'pass2' => $_POST['pass2'],
);
if ( $_FILES['avatar']['error'] === 0 ) :
$data['avatar'] = new \CurlFile( $_FILES["avatar"]['tmp_name'], $_FILES["avatar"]['type'], $_FILES["avatar"]['name']);
endif;
$url = API_BASE_URL.'wp-json/lbcrm/v1/user';
$token = $current_user->access_token;
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST,1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer $token",
"content-type: multipart/form-data;"
) );
$obj = json_decode( curl_exec( $ch ) );
curl_close( $ch );
$respond = $obj;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment