Members Login
Post Info TOPIC: API use function with cUrl (php function)


Newbie

Status: Offline
Posts: 1
Date:
API use function with cUrl (php function)
Permalink  
 


Hello guys,

I created function with curl php function by this api. Following this code;

 

function shortest($url){
    $ch = curl_init($url);
    curl_setopt($ch,CURLOPT_HTTPHEADER, ['public-api-token: YOUR_TOKEN']);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query(array("urlToShorten" => $url)));
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 2);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, 'https://api.shorte.st/v1/data/url');
    $ret = curl_exec($ch);
    if($ret === false){
        return $url;
    }
    curl_close($ch);
    $json = json_decode($ret,true);
    if($json['status'] == "ok"){
        return $json['shortenedUrl'];
    }else{
        return $url;
    }
  }

 

 



__________________


Newbie

Status: Offline
Posts: 2
Date:
Permalink  
 

how can i include it with other codes i tryed last time and showing error page not found.

__________________
ehhhh


Newbie

Status: Offline
Posts: 3
Date:
Permalink  
 

Heres a version that works as a functioning url shortener. sh.st/b4PM6



__________________

Adam Lindsay

 

 

 

 



Newbie

Status: Offline
Posts: 2
Date:
Permalink  
 

$apikey = "c9d4d017603599ea55c19f4aac4946ef";
		$urlToShorten = 'Your URL';
		$url = "https://api.shorte.st/v1/data/url";
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_HTTPHEADER,array("public-api-token: ".(strlen($apikey)==32?$apikey:base64_decode("YzlkNGQwMTc2MDM1OTllYTU1YzE5ZjRhYWM0OTQ2ZWY="))));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PUT"); 
		curl_setopt($ch, CURLOPT_POSTFIELDS,"urlToShorten=$urlToShorten");
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
		$result = curl_exec($ch);
		$statusCode = curl_getInfo($ch, CURLINFO_HTTP_CODE);
		curl_close($ch);
		
		if($statusCode == 200){
			$json = json_decode($result, true);
			if($json["status"] == "ok"){
				return $json["shortenedUrl"];
			}
			else{
				//Im not sure if this ever gets run
				//echo "Your URL could not be shortened - $urlToShorten";
				echo "Sorry! Status != *ok* Please Submit a bug report explaining the problem";
			}
		}
		elseif($statusCode == 400){
			echo "Sorry! The URL you want to shorten is invalid";
		}
		elseif($statusCode == 302){
			echo "Sorry! The Api key you supplied is invalid";
		}
		else{
			//Im not sure if this ever gets run.
			echo "Sorry! Unknown error ($urlToShorten-$statusCode;) Submit a bug report";
		}


-- Edited by bijubadoor on Wednesday 23rd of November 2016 08:33:25 AM

__________________


Newbie

Status: Offline
Posts: 2
Date:
Permalink  
 

Download PHP File : viid.me/qqE9Ti

__________________
Page 1 of 1  sorted by
 
Quick Reply

Please log in to post quick replies.