Jump to content

Recommended Posts

Hi there :D I am making a PHP url shortener using about 4-5 APIs from different websites (to.ly, sn.im, goo.gl, is.dg) and I need to combine them into one PHP script... and also the form lets the user pick which url shortener they want.

 

to.ly:

<?php
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($_POST[url]); // Test
?>

 

sn.im:

<?php 

// REQUIRED FIELDS 
$sniplink  = $_POST[url];  // THE URL TO BE SNIPPED 
$snipuser  = 'alexpja';            // YOUR USER ID REQUIRED
$snipapi   = '';               // FIND IN YOUR "SETTINGS" PAGE

// OPTIONAL FIELDS 
$snipnick   = '';            // MEANINGFUL NICKNAME FOR SNIPURL 
$sniptitle  = '';  // TITLE IF ANY
$snippk     = '';                      // PRIVATE KEY IF ANY
$snipowner  = '';                      // IF THE SNIP OWNER IS SOMEONE ELSE
$snipformat = 'simple';                      // DEFAULT RESPONSE IS IN XML, SEND "simple" 
                                       // FOR JUST THE SNIPURL  
$snipformat_includepk = "";            // SET TO "Y" IF YOU WANT THE PRIVATE KEY
                                       // RETURNED IN THE SNIPURL ALONG WITH THE ALIAS                                       

//----------------------------------
// NO NEED TO EDIT BEYOND THIS POINT
//----------------------------------
$URL        = 'http://snipr.com/site/getsnip';
$sniplink   = rawurlencode($sniplink);
$snipnick   = rawurlencode($snipnick);
$sniptitle  = rawurlencode($sniptitle);

// POSTFIELD
$postfield =  'sniplink='  . $sniplink  . '&' .
              'snipnick='  . $snipnick  . '&' .
              'snipuser='  . $snipuser  . '&' .
              'snipapi='   . $snipapi   . '&' .
              'sniptitle=' . $sniptitle . '&' .
              'snipowner=' . $snipowner . '&' .
              'snipformat='. $snipformat. '&' .
              'snippk='    . $snippk   
  ; 
       
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfield);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
       
// THIS IS MERELY FOR DISPLAY IN A BROWSER
// TO GET THE RESPONSE DATA, JUST USE $data         
// echo '<pre>', htmlentities($data), '</pre>';    
echo $data;

?>

 

Help?? and should I name the <option> like... <option name="is.dg">?

 

Link to comment
https://forums.phpfreaks.com/topic/227406-php-function-help/
Share on other sites

Have an HTML select/option form. Switch over the result in PHP, call the appropriate shorten function.

 

I can do the form, except I do not now how I would switch over the result and call the function when I have 4 diff. APIs and I need them all in one script... should I just put all of them together in the script?

Link to comment
https://forums.phpfreaks.com/topic/227406-php-function-help/#findComment-1172995
Share on other sites

Put each of the API's in a separate file.  After the form selection is passed, call in which file you need.  Make sure you pass the same variables to each script, and receive the same variables back.

 

Everything should go seamlessly.

Link to comment
https://forums.phpfreaks.com/topic/227406-php-function-help/#findComment-1173000
Share on other sites

Put each of the API's in a separate file.  After the form selection is passed, call in which file you need.  Make sure you pass the same variables to each script, and receive the same variables back.

 

Everything should go seamlessly.

 

I'd do that if I knew HOW to... could you explain it even further with some code??

Link to comment
https://forums.phpfreaks.com/topic/227406-php-function-help/#findComment-1173001
Share on other sites

I think what they're saying is have them pick which shortener they want using the select list.  Then just build a case to use the appropriate one.  You can't really combine them all together, since they're different APIs.  You can step through each one, create the shortened link, and make all of them available for the user to select which one to use. 

 

Link to comment
https://forums.phpfreaks.com/topic/227406-php-function-help/#findComment-1173002
Share on other sites

I think what they're saying is have them pick which shortener they want using the select list.  Then just build a case to use the appropriate one.  You can't really combine them all together, since they're different APIs.  You can step through each one, create the shortened link, and make all of them available for the user to select which one to use.

 

"Then just build a case to use the appropriate one." ~ ?

 

"You can't really combine them all together, since they're different APIs." ~ That's not what I meant, I meant like put them all in the same script not like combine them all... sorry for the weird wording... used to typing in Polish :S

 

 

Link to comment
https://forums.phpfreaks.com/topic/227406-php-function-help/#findComment-1173004
Share on other sites

I brainfarted...I meant switch, not case. 

 

Assuming you're using straight PHP and not jQuery/javascript/AJAX....

 

On one page, make a form with an input field for them to paste the URL and a select list where they can choose which shortener to use.  When they hit select (or use onchange on the select...) take them to the page that contains the code for all 4 shorteners.  Using a switch, just look at the $_POST variables and pass them to your APIs as appropriate and output the shortened URL.  More info on switches in php is here:  http://php.net/manual/en/control-structures.switch.php

 

OR

 

You can just take the URL and pass it to the page which in turn uses each API to shorten it and let them select which one to cut/paste for their use. 

Link to comment
https://forums.phpfreaks.com/topic/227406-php-function-help/#findComment-1173008
Share on other sites

I brainfarted...I meant switch, not case. 

 

Assuming you're using straight PHP and not jQuery/javascript/AJAX....

 

On one page, make a form with an input field for them to paste the URL and a select list where they can choose which shortener to use.  When they hit select (or use onchange on the select...) take them to the page that contains the code for all 4 shorteners.  Using a switch, just look at the $_POST variables and pass them to your APIs as appropriate and output the shortened URL.  More info on switches in php is here:  http://php.net/manual/en/control-structures.switch.php

 

OR

 

You can just take the URL and pass it to the page which in turn uses each API to shorten it and let them select which one to cut/paste for their use.

 

Will try to do the first method but for now I'm sticking with the first one... thanks! :)

Link to comment
https://forums.phpfreaks.com/topic/227406-php-function-help/#findComment-1173012
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.