alexpja Posted February 11, 2011 Share Posted February 11, 2011 Hi there 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">? Quote Link to comment https://forums.phpfreaks.com/topic/227406-php-function-help/ Share on other sites More sharing options...
Pikachu2000 Posted February 11, 2011 Share Posted February 11, 2011 And do you have a question to go along with that code? Quote Link to comment https://forums.phpfreaks.com/topic/227406-php-function-help/#findComment-1172985 Share on other sites More sharing options...
alexpja Posted February 11, 2011 Author Share Posted February 11, 2011 And do you have a question to go along with that code? Yes, I'd like to know how can I combine all of the API codes I have into one script so the user can shorten their link. Quote Link to comment https://forums.phpfreaks.com/topic/227406-php-function-help/#findComment-1172986 Share on other sites More sharing options...
lastkarrde Posted February 11, 2011 Share Posted February 11, 2011 Have an HTML select/option form. Switch over the result in PHP, call the appropriate shorten function. Quote Link to comment https://forums.phpfreaks.com/topic/227406-php-function-help/#findComment-1172991 Share on other sites More sharing options...
alexpja Posted February 11, 2011 Author Share Posted February 11, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/227406-php-function-help/#findComment-1172995 Share on other sites More sharing options...
jcbones Posted February 11, 2011 Share Posted February 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227406-php-function-help/#findComment-1173000 Share on other sites More sharing options...
alexpja Posted February 11, 2011 Author Share Posted February 11, 2011 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?? Quote Link to comment https://forums.phpfreaks.com/topic/227406-php-function-help/#findComment-1173001 Share on other sites More sharing options...
jcanker Posted February 11, 2011 Share Posted February 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227406-php-function-help/#findComment-1173002 Share on other sites More sharing options...
alexpja Posted February 11, 2011 Author Share Posted February 11, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/227406-php-function-help/#findComment-1173004 Share on other sites More sharing options...
jcanker Posted February 11, 2011 Share Posted February 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227406-php-function-help/#findComment-1173008 Share on other sites More sharing options...
alexpja Posted February 11, 2011 Author Share Posted February 11, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/227406-php-function-help/#findComment-1173012 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.