如何使用 PHP API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17234659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:33:36  来源:igfitidea点击:

How to use a PHP API

phpapi

提问by Benedict Lewis

There is a really simple API at http://mee.la/api.php, and I was wondering if anyone could help me use it. You can read more about the API at http://mee.la/api-about.phpbut all it is is a call to http://mee.la/api.php?url=http://test.com/, which returns http://mee.la/2755. I am trying to create a PHP page which when loaded makes said call, and prints the response. Any ideas how I should start because I'm only just starting to learn PHP,

http://mee.la/api.php有一个非常简单的 API ,我想知道是否有人可以帮助我使用它。您可以在http://mee.la/api-about.php阅读有关 API 的更多信息,但这只是对http://mee.la/api.php?url=http://test.com的调用/,返回http://mee.la/2755。我正在尝试创建一个 PHP 页面,该页面在加载时进行所述调用,并打印响应。任何我应该如何开始的想法,因为我才刚刚开始学习 PHP,

回答by jcubic

You can do something like this, put input box and a form

你可以做这样的事情,把输入框和一个表单

<form action="your_script.php" method="POST">
   <input type="edit" name="address"/>
   <input type="submit" value="go"/>
</form>

and in PHP use curl to get the URL from the API

并在 PHP 中使用 curl 从 API 获取 URL

if (isset($_POST['address'])) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://mee.la/api.php?url=' . 
        urlencode($_POST['address']));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);
}

then you can print the URL, create html link or redirect to that new URL using header

然后您可以打印 URL、创建 html 链接或使用标题重定向到该新 URL

header('Location: ' . $data);

回答by clagio

$myUrl="http://test.com";
$shortUrl=file_get_contents("http://mee.la/api.php?url=".$myUrl);
echo  $shortUrl;

回答by Nanhe Kumar

First Way
    echo file_get_contents('http://mee.la/api.php?url=http://test.com/');

Second Way
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => 'http://mee.la/api.php?url=http://test.com/',

    ));
    $resp = curl_exec($curl);
    curl_close($curl);
    echo $resp;