Import an XML or CSV list of URLs to Tr.im to be automatically shortened

Important: Tr.im API is in Beta

This API will help to automatically shorten URL list given in XML or CSV format.
For shortening bulk URLs in XML or CSV format you need to perform following steps.

Url: /links/upload
Method: POST


Request

1. Create one PHP file in root directory. e.g. script.php
2. Add following code snippet in that PHP file.

<?php

function sendFile($fullPath, $host)
{
    if (function_exists('curl_file_create')) {
        $cFile = curl_file_create($fullPath);
    } else {
        $cFile = '@'.realpath($fullPath);
    }
    $post   = ['fileWithLinks' => $cFile];
    $ch     = curl_init();
    curl_setopt($ch, CURLOPT_URL, $host.'/links/upload');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'x-api-key: 0b4e0eb22e931964d5ceaac01098312a' // Change to your x-api-key
    ]);
    $result = curl_exec($ch);
    var_dump(curl_error($ch));
    curl_close($ch);
    return $result;
}

$fileWithLinks = '/home/sushil/Desktop/links.xml'; // Change to your file path

$result = sendFile($fileWithLinks, 'https://stage.tr.im');
echo $result;

3. Modify x-api-key in above PHP file to your x-api-key key.
4. Modify $fileWithLinks to path or xml/csv files with links.
5. Then from terminal run php script.php (Change script.php with your script name)


An example response


string(0) ""
{"links":[{"clicksCount":0,"id":1835206,"short_url":"paper22.com\/xmlupload1","long_url":"http:\/\/www.firstpost.com\/politics\/narendra-modis-topi-politics-is-all-about-appeasing-his-voter-base-1480751.html\n            ","user_id":{"id":10429},"client_ip":"114.143.44.144","is_archived":0,"is_spam":false,"page_title":"","created_at":{"days":["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],"relative_keywords":["this","next","last","tomorrow","yesterday","+","-","first","last","ago"],"to_string_format":"Y-m-d H:i:s"},"targets":[],"link_aliases":[],"link_tags":[]},{"clicksCount":0,"id":1835207,"short_url":"rstackhouse.com\/xmlupload2","long_url":"http:\/\/www.firstpost.com\/category\/blogs","user_id":{"id":10429},"client_ip":"114.143.44.144","is_archived":0,"is_spam":false,"page_title":"News Blogs, Expert Blogs India & World, Opinions- Firstpost","created_at":{"days":["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],"relative_keywords":["this","next","last","tomorrow","yesterday","+","-","first","last","ago"],"to_string_format":"Y-m-d H:i:s"},"targets":[],"link_aliases":[],"link_tags":[]},{"clicksCount":0,"id":1835200,"short_url":"stage.tr.im\/xmlupload3","long_url":"http:\/\/www.firstpost.com\/category\/sports","user_id":{"id":10429},"client_ip":"114.143.44.144","is_archived":0,"is_spam":false,"page_title":"Latest Sports News, Live Cricket Sports News, Today's India Sports News, Score, Updates & Analysis - Firstpost","created_at":"2015-09-10T05:54:33+0000","targets":[],"link_aliases":[],"link_tags":[]},{"clicksCount":0,"id":1835201,"short_url":"stage.tr.im\/KoZrt","long_url":"http:\/\/www.firstpost.com\/category\/india","user_id":{"id":10429},"client_ip":"114.143.44.144","is_archived":0,"is_spam":false,"page_title":"India News, Latest News in India, Latest Breaking News India, Indian States & Cities Headlines - Firstpost","created_at":"2015-09-10T05:54:33+0000","targets":[],"link_aliases":[],"link_tags":[]}]}
Have more questions? Submit a request

Comments

Powered by Zendesk