0

I intend to use the api for a link shortening service, the api address for this site is as follows:

http://mitly.ir/api.php

And for example for this request:

http://mitly.ir/api.php?url=https://google.com

This outputs json:

{
    "longurl": "https:\/\/google.com",
    "shorturl": "http:\/\/mitly.ir\/12MxP",
    "stats": "http:\/\/mitly.ir\/stats.php?id=12MxP"
}

Now I wrote the following code to use this api, but unfortunately the output that returns to me is null:

//API Url
  $url = 'http://mitly.ir/api.php';
  //Initiate cURL.
  $ch = curl_init($url);
  //Tell cURL that we want to send a POST request.
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_HEADER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, "url=https://google.com");
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
  //Execute the request
  $result = curl_exec($ch);
  //echo $result;
  $response = json_decode($result, true);
  var_dump($response);
  echo "your url is :".$response['result']['shorturl'];

Please help me solve this problem

h1h2
  • 85
  • 2
  • 11

1 Answers1

1

from your example you need GET request:

$short = 'https://google.com';
$url = 'http://mitly.ir/api.php?url='.$short;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
    $result = curl_exec($ch);
    $response = json_decode($result, true);
    var_dump($response);
Eden Moshe
  • 1,097
  • 1
  • 6
  • 16
  • still returns null – h1h2 Dec 27 '20 at 11:35
  • 1
    You need to combine this with the processing on the duplicate (i.e. the answer [here](https://stackoverflow.com/a/12510094/1213708) ) – Nigel Ren Dec 27 '20 at 11:44
  • function remove_utf8_bom($text) { $bom = pack('H*','EFBBBF'); $text = preg_replace("/^$bom/", '', $text); return $text; } see: https://stackoverflow.com/questions/10290849/how-to-remove-multiple-utf-8-bom-sequences/15423899#15423899 $response = json_decode(remove_utf8_bom($result), true); – Eden Moshe Dec 27 '20 at 12:10