使用 Bitly API 缩短 URL

发布于 2024-11-03 16:32:48 字数 1283 浏览 3 评论 0原文

我在 网站上找到了下面的 Bitly API 代码。我很难让它创建并回显名为 $fullurl 的变量的稍微缩短的 URL。我该怎么做呢?

编辑: 没有出现错误代码,只是没有显示稍微缩短的 URL。

编辑 2: var_dump($response); 返回 NULL

编辑 3: 我确实用我的 API 登录名和密钥替换了 API 登录名和密钥。

编辑4:我在原始教程的评论之一中找到了答案。对于所有 PHP 专业人士来说,我的问题太基础了:我只需要在末尾添加 echo bitly_shorten($fullurl); 即可。

预先感谢,

约翰

function bitly_shorten($url)
{
    $query = array(
        "version" => "2.0.1",
        "longUrl" => $url,
        "login" => API_LOGIN, // replace with your login
        "apiKey" => API_KEY // replace with your api key
    );

    $query = http_build_query($query);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://api.bit.ly/shorten?".$query);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $response = curl_exec($ch);
    curl_close($ch);

    $response = json_decode($response);

    if($response->errorCode == 0 && $response->statusCode == "OK") {
        return $response->results->{$url}->shortUrl;
    } else {
        return null;
    }
}

I found the Bitly API code below on this site. I'm having a hard time getting it to create and then echo a Bitly shortened URL for a variable called $fullurl. How would I do that?

EDIT: No error code appears, just no bitly shortened URL is shown.

EDIT 2: var_dump($response); returns NULL

EDIT 3: I did replace the API login and key with my mine.

EDIT 4: I found the answer in one of the comments on the original tutorial. My question was too basic for all you PHP pros: I simply needed to add echo bitly_shorten($fullurl); at the end.

Thanks in advance,

John

function bitly_shorten($url)
{
    $query = array(
        "version" => "2.0.1",
        "longUrl" => $url,
        "login" => API_LOGIN, // replace with your login
        "apiKey" => API_KEY // replace with your api key
    );

    $query = http_build_query($query);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://api.bit.ly/shorten?".$query);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $response = curl_exec($ch);
    curl_close($ch);

    $response = json_decode($response);

    if($response->errorCode == 0 && $response->statusCode == "OK") {
        return $response->results->{$url}->shortUrl;
    } else {
        return null;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阳光①夏 2024-11-10 16:32:48

将其更改为:

function bitly_shorten($url){
  $query = array(
    "version" => "2.0.1",
    "longUrl" => $url,
    "login" => API_LOGIN, // replace with your login
    "apiKey" => API_KEY // replace with your api key
  );

  $query = http_build_query($query);

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "http://api.bitly.com/v3/shorten?".$query);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  $response = curl_exec($ch);
  curl_close($ch);

  $response = json_decode($response);
  if( $response->status_txt == "OK") {        
    return $response->data->url;
  } else {
    return null;
  }
}

Change it to:

function bitly_shorten($url){
  $query = array(
    "version" => "2.0.1",
    "longUrl" => $url,
    "login" => API_LOGIN, // replace with your login
    "apiKey" => API_KEY // replace with your api key
  );

  $query = http_build_query($query);

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "http://api.bitly.com/v3/shorten?".$query);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  $response = curl_exec($ch);
  curl_close($ch);

  $response = json_decode($response);
  if( $response->status_txt == "OK") {        
    return $response->data->url;
  } else {
    return null;
  }
}
情域 2024-11-10 16:32:48

bit.ly 似乎更新了他们的 api ,请访问

http:// code.google.com/p/bitly-api/wiki/ApiDocumentation#Authentication_and_Shared_Parameters

for api..

url 似乎是这样的, http://api.bitly.com/v3/shorten?.....

他们所说的新版本是 3,在您的代码中是 2.0.1

每当您使用在线服务的 api 时,最好从他们的网站获取它,而不是从任何第三方网站或博客获取。

It seems that bit.ly had updated their api , please visit

http://code.google.com/p/bitly-api/wiki/ApiDocumentation#Authentication_and_Shared_Parameters

for api..

The url seems to be something like this, http://api.bitly.com/v3/shorten?.....

The new version they stated is 3 and in your code its 2.0.1

Whenever you are using an api of an online service its better to get it from their site instead of getting that from any third party site or blog..

蓝颜夕 2024-11-10 16:32:48

我在原始教程的评论之一中找到了答案。对于所有 PHP 专业人士来说,我的问题太基础了:我只需要在末尾添加 echo bitly_shorten($fullurl); 即可。

I found the answer in one of the comments on the original tutorial. My question was too basic for all you PHP pros: I simply needed to add echo bitly_shorten($fullurl); at the end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文