phpyourls

yourls api is executing url before shorting


I have installed YOURLS API and its working fine except one problem. Whatever URL I am passing to shorten, its first executing that URL. One of the links is un-subscribe link clicking on which instantly it un-subscribe the member from our mailer. I am using passwordless API call with security token and I am calling yourls-api.php file by file_get_contents function. Here is the code snippet I have written:

$url = urlencode($url);
$api_url = 'http://mydomain.com/yourls/yourls-api.php?signature=593642y63x&action=shorturl&format=json&url='.$url;
$arr_output = json_decode(file_get_contents($api_url), true);   

Can anybody tell me how can I stop YOURLS API to not execute URL before shortening?


Solution

  • Sorry for the delay was bit busy during this weekend.

    Just figured out this morning why you are facing the mentioned problem.

    IN your code:

    $url = urlencode($url);
    $api_url = 'http://mydomain.com/yourls/yourls-api.php?signature=593642y63x&action=shorturl&format=json&url='.$url;
    $arr_output = json_decode(file_get_contents($api_url), true);
    

    you are passing following parameters: signature, action, url and format but the YOURLS api does not count these parameters to be enough it needs one more parameter to be passed along and i.e title

    So you will need to modify your code to include "title" parameter along with all the other parameters you are passing, something like this:

    $url = urlencode($url);
    $api_url = 'http://mydomain.com/yourls/yourls-api.php?title=swati&signature=593642y63x&action=shorturl&format=json&url='.$url;
    $arr_output = json_decode(file_get_contents($api_url), true);
    

    Now, the question arises why not passing the title parameter was causing the URL to be executed? So, this is because if the title parameter is excluded then the YOURLS make an extra HTTP call to the provided URL to fetch the title from that URL.

    Have a good day.