Jump to content

TheAccountant

Friends
  • Joined

  • Last visited

Solutions

  1. TheAccountant's post in Problem with sending an object using REST API was marked as the answer   
    I was able to solve it by making it in PHP and pass the whole information in one array. Before I was trying to link the form directly to API.
    The best way I managed to handle it is by making the form send its request to a script I wrote and then send the information to API.
    First collect all the fields of the information (fields object ) and then create an array to put all required fields from API along with field object it self.
     
    This is my code:
    First you prepare the setup of your API
    $communityUrl = 'https://www.domain.com/'; $apiKey = '00000000000000000000000000000000'; $endpoint = '/cms/records/7'; // 7 is my database id Then you receive the submitted data, handle it and store it in one associated array by making the key same is field id of each one of them. Try to make sure that field assigned as "Title" and "Content" are there.
    $fields['28'] = $_POST['fullname']; // This is assigned as title $fields['32'] = $_POST['age']; $fields['29'] = $_POST['experience']; // this is assigned as content $fields['35'] = $_POST['previousjob']; $fields['33'] = $_POST['worklocations']; $fields['34'] = $_POST['clients']; $fields['30'] = $_POST['whatsapp']; $fields['31'] = $_POST['email']; $field['FIELD_ID_FROM_YOUR_DATABASE_HERE'] = ... Then you put everything together in one new array ( required fields from API + fields object )
    $data = array( "author" => AUTHOT_ID_FROM_YOUR_COMMUNITY, "category" => CATEGORY_ID_FROM_YOUR_DATABASE, "fields" => $fields, // Our field object ); Finally you send your request to API.
    $curl = curl_init( $communityUrl . 'api' . $endpoint ); curl_setopt_array( $curl, array( CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERPWD => "{$apiKey}:", CURLOPT_USERAGENT => "MyUserAgent/1.0", CURLOPT_POSTFIELDS => http_build_query($data), ) ); $response = curl_exec( $curl ); curl_close($curl); if($response){ echo 'Ok'; exit; }else{ echo 'Error'; exit; }