1

I have a website which has a feature to allow users to create shortened URLs. To handle the URL shortening I am using YOURLS (an open-source URL shortener) which I run on a different server of mine and connect the two sites through API calls.

I have created a YOURLS plugin with some custom API actions so when a user creates a short URL it will attach their user ID from my website to the yourls_url table as shown below:

enter image description here

When a user visits their "My Links" page I need to make an API call to return all of the short URL's created by that user. To do this it should just be as simple as creating a filter for "api_result_stats".

This is the filter I have created which is suppose to only return the URLs created by the specified user:

yourls_add_filter( 'api_result_stats', 'separate_users_api_stats' );

function separate_users_api_stats() {
    global $ydb;

    $user = ( isset( $_REQUEST['user'] ) ? $_REQUEST['user'] : '' );

    $table = YOURLS_DB_TABLE_URL;
    $result = $ydb->query("SELECT * FROM `$table` WHERE  `user` = '" . $user . "'");

    return $result > 0;

}

To test this I can try http://example.com/yourls-api.php?action=stats&user=test123&limit=10&filter=last&signature=xxxxxxxxxx but unfortunately all I receive is:

enter image description here

I'm not too familiar with PHP/MYSQL so I've been stuck on this for quite some time now, if anyone has any ideas as to where I'm going wrong or could offer an example it would be greatly appreciated!

U54
  • 122
  • 1
  • 9
  • Hi U54! I see that you are using PHPMyAdmin. The first step in troubleshooting will be to manually check if the query you want to run is returning data. Open the SQL tab and run this query: SELECT keyword FROM yourls_url WHERE user = 'test123'; Let me know if you get some rows returned. You may also consider changing what the separate_users_api_stats() function returns. I notice that "return $result > 0" will simply return TRUE if you got rows from the database and FALSE if you didn't. – Matthew May 24 '16 at 05:24
  • @Matthew Thank you very much for the reply! I have ran the query and it appears that everything is working correctly, it returned all of the expected data. [Here](https://github.com/YOURLS/YOURLS/blob/1481bef4ef0ad2686b80db1d9982e29df96086c8/includes/functions-api.php#L167) is the yourls_api_stats function in the YOURLS code that I am attaching the separate_users_api_stats filter to. By having it return true or false I believe this would allow me to only return the rows which are "true" which in this case would only return the rows created by that user, no? – U54 May 24 '16 at 06:09
  • I'm so sorry for not responding sooner! I've been very busy. Since the query returns data then your function returning true or false is the reason why you're not getting that data after you call the function. **return $result > 0;** is asking PHP "Did I get results?" and PHP saying, "Yes." So, you see that you asked a simple yes/no question in your return line instead of giving back the actual data. Change it to **return $result;** to get the actual data after calling your function. – Matthew May 27 '16 at 18:57
  • Also, your actual SQL query already includes ***WHERE `user` = ...***. That is the user filter; the database is already only giving you rows where the user is **$user**. – Matthew May 27 '16 at 19:09
  • 1
    Forgot to update the question but I figured it out, thank you so much for the reply/help though, really helped me understand how it's working! – U54 May 30 '16 at 21:19

1 Answers1

1

Here is what solved this for me:

yourls_add_filter( 'api_result_stats', 'separate_users_api_stats' );

function separate_users_api_stats() {
    global $ydb;

    $user = ( isset( $_REQUEST['user'] ) ? $_REQUEST['user'] : '' );

    $table = YOURLS_DB_TABLE_URL;

    $query = "SELECT * FROM `$table` WHERE `user` = '" . $user . "'";

    $result = $ydb -> get_results($query);

    return $result;

}
U54
  • 122
  • 1
  • 9