• Page 1 of 1
  • 1
uCoz Community » Additional Services » PHP and API on uCoz » Problem with uAPI
Problem with uAPI
BraziluCoz
Posts: 84
Reputation: 1

Message # 1 | 4:04 AM
Hello everyone.

I need emails from registred users in my website for an App.

First I had tried default API (myws.ucoz.com/api/*), but it displays user email only if an administrator is logged in. It is good because is safe. But if I use API KEY to access is safe, right? Not for uCoz! Even though I am using API KEY, user's email is not showed to me.

I found the solution using uAPI, I can get user's email by username or user ID, however it fails in some cases.

I am using this example http://api.ucoz.net/ru/manual/users#two

For some user get all details, for other users I get a blank response. I also had tried inform a wrong username and uAPI response to me saying "this user does not exist".

Exists some reason to it does not work (blank response)?

Hello! I am Ennio Sousa
Sunny
Posts: 9296
Reputation: 456

Message # 2 | 7:27 AM
BraziluCoz, please provide your website, the exact code you are using, and examples of user profiles for which the email wasn't retrieved.
I'm not active on the forum anymore. Please contact other forum staff.
BraziluCoz
Posts: 84
Reputation: 1

Message # 3 | 3:36 PM
The website is okanuncie.ucoz.com.br

uAPImodule.php
Code
<?php

error_reporting(E_ERROR);
/*
  Version 1.1
  Date: February 9, 02:00
  1.1 updated: 14 July 2015
  Information about the module: http://api.ucoz.net/ru/manual/module
  On all questions you can address on api@ucoz.net
*/
$oauth_nonce = md5(microtime() . mt_rand()); //не изменять
$timestamp = time(); //не изменять
$sig_method = 'HMAC-SHA1'; //не изменять
$oauth_version = '1.0'; //не изменять
$consumer_key = '';
$consumer_secret = '';
$oauth_token = '';
$oauth_token_secret = '';
$main_url = 'http://www.okanuncie.com.br/uapi'; //you need to specify http:// to uapi. For example: http://yourwebsite.ucoz.ru/uapi
//the setup details is completed

/*
* @function to generate the signed request.
* @$method can take the form of POST, GET, DELETE, PUT.
* @$request_url specifies where to do the request
* @$parametrs parameters to the query
* @$format is not supported, created for a future date improvements (todo)
*/

function uAPIModule($request_url, $method, $parametrs, $format) {
    global $basestring, $consumer_key, $oauth_nonce, $sig_method, $timestamp, $oauth_token, $oauth_version, $hash_key, $oauth_signature, $consumer_secret, $oauth_token_secret, $main_url;

    //begins the signature generation of the correct query
    $request_url = $main_url . mb_strtolower(trim($request_url));
    $method = mb_strtoupper($method);
    $parametrs2 = $parametrs;
    $parametrs = str_replace('@', '', $parametrs);
    $basestring = str_replace('+', '%20', http_build_query($parametrs));
    $basestring = $method . '&' . urlencode($request_url) . '&' . urlencode($basestring);
    $hash_key = $consumer_secret . '&' . $oauth_token_secret;
    $oauth_signature = urlencode(trim(base64_encode(hash_hmac('sha1', $basestring, $hash_key, true))));
    $parametrs_forurl = http_build_query($parametrs);
    $url = $request_url . '?oauth_signature=' . $oauth_signature;
    $url_for = $request_url . '?' . $parametrs_forurl . '&oauth_signature=' . $oauth_signature;

    $curl = curl_init();
    switch ($method) {

        case 'GET':
            curl_setopt($curl, CURLOPT_URL, $url_for);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            $uAPIcurlresult = curl_exec($curl);
            return $uAPIcurlresult;
            break;

        case 'POST':
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $parametrs2);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_VERBOSE, true);
            $uAPIcurlresult = curl_exec($curl);
            return $uAPIcurlresult;
            break;

        case 'PUT':
            $curl = curl_init($url);

            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
            curl_setopt($curl, CURLOPT_POSTFIELDS, $parametrs2);
            $uAPIcurlresult = curl_exec($curl);
            return $uAPIcurlresult;
            break;

        case 'DELETE':
            curl_setopt($curl, CURLOPT_URL, $url_for);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
            $uAPIcurlresult = curl_exec($curl);
            return $uAPIcurlresult;

            break;

        default:
            echo 'Unknown method of transmission!';
            break;
    }
    curl_close($curl);
}

?>


tests.php
Code
<?php

/*
* @autor_name Ennio Sousa
* @autor_site http://uid.me/shzlot
*
* Copyright (c) 2015 Ennio.
* All rights reserved. This program and the accompanying materials
*/
$___notjson = 1;
include "uAPImodule.php";

$username = array(
    'USER_WITH_DATA_RETURNED' => 'juci', //this user exist and return data
    'USER_WITHOUT_DATA_RETURNED' => 'jokkca', //this user exist but return blank data
    'USER_NAME_INVALID' => 'jessica' // this user does not exist
);

/**
* Obter email do usuario
*/
$parametrs = array(
    'oauth_consumer_key' => $consumer_key,
    'oauth_nonce' => $oauth_nonce,
    'oauth_signature_method' => $sig_method,
    'oauth_timestamp' => $timestamp,
    'oauth_token' => $oauth_token,
    'oauth_version' => $oauth_version,
    'user' => $username['USER_NAME_INVALID']
);
ksort($parametrs);
$dados_usuario2 = uAPIModule('/users', 'get', $parametrs, '');
$dados_usuario = json_decode($dados_usuario2);

/**
* Verificar se o usuario existe e mostrar o formulario
*/
print_r($dados_usuario);
?>


Response for ther user jokkca is blank.

Response for an non existing user jessica:
Code
stdClass Object
(
    [error] => stdClass Object
        (
            [msg] => User not found
            [code] => EMPTY_RESPONSE
        )

)





Response for the user juci:
Code
stdClass Object
(
    [users] => Array
        (
            [0] => stdClass Object
                (
                    [board_posts] => 0
                    [uid] => 18
                    [phone] =>
                    [email] => *@ymail.com
                    [ip] => 200.222.204.216
                    [msn] =>
                    [aol] =>
                    [name] => Jucy Amorim
                    [uID] => yes
                    [banned] => no
                    [signature] =>
                    [last_visit] => 2015-01-15 13:23
                    [user] => juci
                    [avatar] =>
                    [photo_posts] => 0
                    [yahoo] =>
                    [reg_date] => 17/11/2014
                    [city] =>
                    [group] => stdClass Object
                        (
                            [name] => Clientes
                            [id] => 3
                        )

                    [icq] =>
                    [publ_posts] => 0
                    [country] => no
                    [dir_posts] => 0
                    [news_posts] => 0
                    [rank] => no
                    [email_verified] => yes
                    [status] => offline
                    [title] =>
                    [forum_posts] => 0
                    [home_page] =>
                    [gender] => Mulher
                    [load_posts] => 0
                    [banrate] => 0
                    [stuff_posts] => 0
                    [birthday] => 0000-00-00
                    [blog_posts] => 0
                    [video_posts] => 0
                    [state] =>
                    [com_posts] => 1
                )

        )

)

Attachments: 3486871.png (75.4 Kb)

Hello! I am Ennio Sousa
Post edited by BraziluCoz - Thursday, 2015-12-17, 3:48 PM
uCoz Community » Additional Services » PHP and API on uCoz » Problem with uAPI
  • Page 1 of 1
  • 1
Search: