使用PHP进行自动twitts的微小URL
I'm trying to implement auto post on Twitter using PHP.
But because of twitt's character limit I wanted to know how can I use tinyURL, instead of placing the full link, like http://www.appdropp.com/ios/stone-age-the-board-game/564247778
You see it's quite long...
I know services like:
- tinyurl.com
- goo.gl
But how can I use these services in bulk, to generate hundreds of links every day with PHP?
我正在尝试使用PHP在Twitter上实现自动发布。 p>
但由于twitt的字符限制,我想知道如何使用tinyURL,而不是放置完整的链接,如 http://www.appdropp.com/ios/stone-age-the-board-game/564247778 p >
你觉得它很长...... p>
我知道的服务如下: p>
- tinyurl。 com li>
- goo.gl li>
ul>
但是如何批量使用这些服务,每天用PHP生成数百个链接? p> div>
You can check Google API, but I'm not sure about that much bulk. I can suggest 3 solutions for you:
- Create short links on your host using PHP.
- Twitter will
shorten the URL for you if you tweet DIRECTLY. But, you cannot do it
using auto-posting. So, count characters (like
message + URL <= 140
) and keep your automatic tweets length less than 140 characters. -
You can also try this (Check the PHP Source Code Example)
function CompressURL($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://"."to.ly/api.php?longurl=".urlencode($url)); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_HEADER, 0); $shorturl = curl_exec ($ch); curl_close ($ch); return $shorturl; } echo CompressURL("http://twitter.com"); // Test
Note that
This method depends upon TinyURL page structure which may be changed in near future and don't use it in that much bulk Or ask them for API ?
You can use this way.
- Encode your url.
use urlencode - Add your encode url to
$url='http://tinyurl.com/create.php?source=indexpage&url=<encoded url>
- Create a dom object
$doc=new DomDocuement();
- Load the page.
$doc->loadHTMLFile($url); // this is page containing shorten url
- Grab the node that contains
shorten url
..Secondblockquote
contains shorten url$nodelist=$doc->getElementsByTagName('blockquote');
$blockquote=$nodelist->item(1) // grabbing shorten url blockquote 0:first 1:second
- Now Grab shorten url :
$shorten_url=$blockquote->$firstChild->NodeValue
-
Use any where you like.
For more info see tiny url page structure
Read more about DOMDocument
For goo.gl, read: https://developers.google.com/url-shortener