When I try to create a form posting via the API I just get an error message “A valid URL was not provided”
This is the code I am using ?
<?php
/*
Template Name: test api */
function calculate_signature( $string, $private_key ) {
$hash = hash_hmac( ‘sha1’, $string, $private_key, true );
$sig = rawurlencode( base64_encode( $hash ) );
return$sig;
}
$base_url = ‘https://xx.xx.com/gravityformsapi/’;
$api_key = ‘xx’;
$private_key = ‘xx’;
$method = ‘POST’;
$route = ‘forms/12/entries’;
$expires = strtotime( ‘+60 mins’ );
$string_to_sign = sprintf( ‘%s:%s:%s:%s’, $api_key, $method, $route, $expires );
$sig = calculate_signature( $string_to_sign, $private_key );
$url = $base_url . $route . ‘?api_key=’ . $api_key . ‘&signature=’ . $sig . ‘&expires=’ . $expires;
var_dump($url);
$entries = array(
array(
‘date_created’ => ‘2018-12-17 11:08:23’,
‘is_starred’ => 0,
‘is_read’ => 0,
‘ip’ => ‘::1’,
‘source_url’ => ‘https://xx.xx.com/emai/?email=267’,
‘currency’ => ‘USD’,
‘created_by’ => 1,
‘user_agent’ => ‘Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0’,
‘status’ => ‘active’,
‘8’ => ‘xx@hotmail.com’,
‘1.3’ => ‘xx’,
‘1.6’ => ‘xx’,
‘3’ => ‘xx@hotmail.com’,
‘5’ => ‘1234’,
‘2’ => ‘The Crows’,
‘4’ => ‘The Crows MSG’,
),
);
//json encode array
$entry_json = json_encode( $entries );
echo"<br/>";
echo"<br/>";
var_dump($entry_json);
//retrieve data
$response = wp_remote_request( urlencode( $url ), array( ‘method’ => ‘POST’, ‘body’ => $entry_json ) );
echo"<br/>";
echo"<br/>";
var_dump($response);
if ( wp_remote_retrieve_response_code( $response ) != 200 || ( empty( wp_remote_retrieve_body( $response ) ) ) ){
//http request failed
die( ‘There was an error attempting to access the API.’ );
}
//result is in the response "body" and is json encoded.
$body = json_decode( wp_remote_retrieve_body( $response ), true );
if( $body[‘status’] > 202 ){
$error = $body[‘response’];
//entry update failed, get error information, error could just be a string
if ( is_array( $error )){
$error_code = $error[‘code’];
$error_message = $error[‘message’];
$error_data = isset( $error[‘data’] ) ? $error[‘data’] : ‘’;
$status = "Code: { $error_code } . Message: { $error_message } . Data: { $error_data } .";
}
else{
$status = $error;
}
die( "Could not post entries. { $status } " );
}
$entry_ids = $body[‘response’];
$entry_ids_created = ‘The following entry ids were created:</br>’;
foreach ( $entry_idsas$entry_id ){
$entry_ids_created .= $entry_id . ‘</br>’;
}