Search code examples
wordpresscurl

WordPress cURL 400


I have been trying to figure out why I am getting a cURL 400 status and how to fix it in WordPress.

Basically, when a user completes a form on the WordPress page and clicks submit, a bunch of functions get triggered.

I just added the function below. I tested the API in Postman and it works fine, but it doesn't want to work from this WordPress site.

Using the PHP Inserter from the WordPress dashboard, I have inserted the following code that makes an API call:

function test(){
  $url     = "https://api.ongage.net/MY_LIST_ID/api/transactional/send";
  $xun = "X_USERNAME: " . MY_USERNAME;
  $xpw = "X_PASSWORD: " . MY_PASSWORD;
  $xac = "X_ACCOUNT_CODE: " . MY_ACCOUNTCODE;
  $content = '{
   "list_id": ,
   "campaign_id": ,
   "recipients": ["[email protected]"],
   "check_status": true,
   "sending_connection_id": MY_SENDING_CONNECTION_ID
  }';

  $curl    = curl_init( $url );
  curl_setopt( $curl, CURLOPT_HEADER, false );
  curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
  curl_setopt( $curl, CURLOPT_HTTPHEADER, array(
    "Content-type: application/json",
    $xun,
    $xpw,
    $xac 
  ) );
  curl_setopt( $curl, CURLOPT_POST, true );
  curl_setopt( $curl, CURLOPT_POSTFIELDS, $content );
  $json_response = curl_exec( $curl );
  $status        = curl_getinfo( $curl, CURLINFO_HTTP_CODE );
  
  if ( $status != 200 && $status != 201 ) {
    $error = error_log( "Error: call to URL $url failed with status $status, 
      response $json_response, curl_error " . curl_error( $curl ) . ", curl_errno " . curl_errno( $curl ) );
    echo '<script language="javascript">';
    echo 'alert("fail "+'.$status.')';
    echo '</script>';
} else {
    echo '<script language="javascript">';
    echo 'alert("success")';
    echo '</script>';
}
  curl_close( $curl );
  $response = json_decode( $json_response, true );
}

In the IF statement above, the alert window shows a 400. I have no idea why, and I am not sure how to to check it.

Please help.

** EDIT **

I made an adjustment to the $content variable in the code above. It now looks like this:

  $content = '{
   "list_id": MY_LIST_ID,
   "campaign_id": MY_CAMAPAIGN_ID,
   "recipients": ["[email protected]"],
   "check_status": true,
   "sending_connection_id": MY_SENDING_CONNECTION_ID
  }';

So I added the list_id and campaign_id, and upon submitting the form, I am now receiving a 404 error.

I really think it has something to do with the contents. I am referencing this page: https://ongage.atlassian.net/wiki/spaces/HELP/pages/947486763/Transactional+Mailing+API+Methods

I hope someone can help me solve this.


Solution

  • I found the problem.

    In the content variable, I have the below:

    $content = '{
     "list_id": MY_LIST_ID,
     "campaign_id": MY_CAMAPAIGN_ID,
     "recipients": ["[email protected]"],
     "check_status": true,
     "sending_connection_id": MY_SENDING_CONNECTION_ID
    }';
    

    "list_id" is supposed to be "message_id". That's what was causing the 404 status. After making that update, I received a 200 response and the transactional message was sent.

    #yay