1

I have an API call I need to make that saves a shopping cart order. To get the contents of the order, it's easy:

https://domain.com/GetCart/{SessionID}

The above URL returns an XML file with the data.

To save the cart contents, though, I'm not sure what I have to do. All the docs say is:

Save the cart utilizing an HTTP Post with the following parameters:

  • session_id
  • first_name
  • last_name
  • city

Etc.

And I have to send that data to:

https://domain.com/SaveCart/{SessionID}

My question is, how do I send that data via HTTP Post? Do I have to put https://domain.com/SaveCart/{SessionID} in the action parameter of a form? E.g.:

<form action="https://domain.com/SaveCart/{SessionID}">

How is sending data to an API via HTTP Post normally done?

5
  • Considering using AJAX to send the data? Commented Feb 9, 2017 at 2:52
  • No AJAX, just HTTP post. Commented Feb 9, 2017 at 3:04
  • 1
    Since you've tagged PHP, you'd be best to run a CURL request. This is a good example of how to do so. Commented Feb 9, 2017 at 3:09
  • Very cool, Darren! I've done HTTP posts using cURL before. Thank you. :) Commented Feb 9, 2017 at 3:29
  • 1
    No worries man :) Commented Feb 9, 2017 at 3:43

1 Answer 1

1

You need to make a cURL call using POST that passes the required fields as post fields.

The curl() function below will do this if you pass it:

$url = 'https://domain.com/SaveCart/{SessionID}';

With {SessionID} replaced by the the session ID and:

$fields[ 'first_name' ] = 'Bob';
$fields[ 'last_name' ]  = 'Smith';
$fields[ 'city' ]       = 'Seattle';

With Bob, Smith, and Seattle replaced with the relevant text;

Then just call the function as:

$result = curl( $url, $fields );

The URLify function below curl() is called by the curl() to transform $fields into the format required for POSTing.

function curl( $url, $fields = FALSE, $encode = TRUE, $tries = 1 ) {

  $ch = curl_init();
  curl_setopt( $ch, CURLOPT_URL, $url );

  if ( ( $fields == FALSE ) ) {
    curl_setopt( $ch, CURLOPT_HEADER, 0 );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  } else {
    if ( is_array( $fields ) ) {
      $fields_string = URLify( $fields, $encode );
      curl_setopt( $ch, CURLOPT_POST, count( $fields ) );
    } else {
      $fields_string = $fields;
      curl_setopt( $ch, CURLOPT_POST, 1 );
    }
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $fields_string );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_TIMEOUT, 60 );
  }

  do {

    $output = curl_exec( $ch );

    $tries = $tries - 1;

    if ( ( curl_errno( $ch ) <> FALSE ) AND ( $tries > 0 ) ) {
      echo 'ERROR in curl: WILL RETRY AFTER 1 SECOND SLEEP! error number: ' . curl_errno( $ch ) . ' error : ' . curl_error( $ch ) . " url: $url";
      sleep( 1 );
    }

  } while ( ( curl_errno( $ch ) <> FALSE ) AND ( $tries > 0 ) );

  // Check if any error occurred
  if ( curl_errno( $ch ) ) {
    echo 'ERROR in curl: NO MORE RETRIES! error number: ' . curl_errno( $ch ) . ' error : ' . curl_error( $ch ) . " url: $url";
  }

  curl_close( $ch );
  return $output;

}

This function is called by the curl() function:

function URLify( $arr, $encode = FALSE ) {

    $fields_string = '';
    foreach( $arr as $key => $value ) {
        if ( $encode ) {
      $key = urlencode( $key );
            $value = urlencode( $value );
        }
        $fields_string .= $key . '=' . $value . '&';
    }
    $fields_string = substr( $fields_string, 0, ( strlen( $fields_string ) - 1 ) );

    return $fields_string;

}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for the detailed explanation.
@GTSJoe Glad I could help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.