Home » Php » PHP Twitter – Library tmhOAuth: Error prepare params

PHP Twitter – Library tmhOAuth: Error prepare params

Posted by: admin February 25, 2020 Leave a comment


I’m using, a long time ago, a library (something old), to integrate twitter with php. It is tmhOAuth from themattharris (version 0.53).
No problems until December 2019, but without changing anything from the library or server side, I now have problems with the function that prepares the parameters when an “array of array” is sent.

The function:

  private function prepare_params($params) {
    // do not encode multipart parameters, leave them alone
    if ($this->config['multipart']) {
      $this->request_params = $params;
      $params = array();

    // signing parameters are request parameters + OAuth default parameters
    $this->signing_params = array_merge($this->get_defaults(), (array)$params);
    // Remove oauth_signature if present
    // Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.")
    if (isset($this->signing_params['oauth_signature'])) {

    // Parameters are sorted by name, using lexicographical byte value ordering.
    // Ref: Spec: 9.1.1 (1)
    uksort($this->signing_params, 'strcmp');

    // encode. Also sort the signed parameters from the POST parameters
    foreach ($this->signing_params as $k => $v) {
        $k = $this->safe_encode($k);
        $v = $this->safe_encode($v);

        $_signing_params[$k] = $v;
        $kv[] = "{$k}={$v}";

    // auth params = the default oauth params which are present in our collection of signing params
    $this->auth_params = array_intersect_key($this->get_defaults(), $_signing_params);
    if (isset($_signing_params['oauth_callback'])) {
      $this->auth_params['oauth_callback'] = $_signing_params['oauth_callback'];

    // request_params is already set if we're doing multipart, if not we need to set them now
    if ( ! $this->config['multipart'])
      $this->request_params = array_diff_key($_signing_params, $this->get_defaults());

    // create the parameter part of the base string
    $this->signing_params = implode('&', $kv);

  private function safe_encode($data) {
    if (is_array($data)) {
      return array_map(array($this, 'safe_encode'), $data);
    } else if (is_scalar($data)) {
      return str_ireplace(
        array('+', '%7E'),
        array(' ', '~'),
    } else {
      return '';

The error is in $kv[] = "{$k}={$v}", it gives:

Array to string conversion

An example of a parameter that fails:

k =event=
v =Array
    [type] => message_create
    [message_create] => Array
            [target] => Array
                    [recipient_id] => 1866814
            [message_data] => Array
                    [text] => test
  • How could you improve the function to handle this type of parameters?
  • What could change that did not give error until a few months ago and now yes?, being that it is some basic connection php no twitter

I appreciate any help.

How to&Answers: