PHP не является моим основным языком программирования, поэтому я много проб и ошибок попробуйте заставить это работать.
Ниже приведен мой код в файле Themes Functions.php
Код: Выделить всё
add_action( 'gform_after_submission', 'post_to_third_party', 10, 2 );
function post_to_third_party( $entry, $form ) {
$endpoint_url = '[Correct API Endpoint]';
$body = array(
'prospectFirstName' => $entry['18'],
// 'prospectLastName' => $entry['19'],
// 'prospectEmail' => $entry['2'],
// 'prospectPhone' => $entry['5'],
// 'notes' => $entry['3'],
'source' => 'Website',
'communityId' => 32
);
$args = array(
'headers' => [
'accept' => 'text/plain',
'Authorization' => 'Basic ' . base64_encode( 'Correct-username:Correct-password' ),
'Content-Type' => 'application/json'
],
'method' => 'POST',
'body' => $body
);
GFCommon::log_debug( 'gform_after_submission: JSON Encode => ' . json_encode($args, JSON_UNESCAPED_SLASHES) );
$response = wp_remote_get( $endpoint_url, json_encode($args) );
// $response = wp_remote_post( $endpoint_url, json_encode($args) );
GFCommon::log_debug( 'gform_after_submission: response => ' . print_r( json_encode($response), true ) );
GFCommon::log_debug( 'gform_after_submission: response => ' . print_r( $response, true ) );
$response = wp_remote_get( $endpoint_url, json_encode($args) );
$response = wp_remote_get( $endpoint_url, json_encode($args, JSON_UNESCAPED_SLASHES) );
$response = wp_remote_get( $endpoint_url, $ args);
Когда я не использую json_encode, я получаю сообщение об ошибке
Код: Выделить всё
"errors":{"$":["'p' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0."]
Спасибо
Подробнее здесь: https://stackoverflow.com/questions/790 ... authorized