我有一个问题,是否有可能。
我已经遵循了设置:
一个服务器与它在一个服务器与php园艺文件,php在其上运行。现在我想在本地服务器上设置load然后调用函数
服务器:API.php
<?php
function random_password() {
$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
$pass = array(); //remember to declare $pass as an array
$alpha_length = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 8; $i++) {
$n = rand(0, $alpha_length);
$pass[] = $alphabet[$n];
}
return implode($pass); //turn the array into a string
}
?>
地方:test_locaal.php
<?php
include "http://***api.new*******.nl/API.php";
print_r(random_password());
?>
我得到以下错误
致命错误:调用未定义函数random_password()在E:WebserverrootAPItest_online.php第5行
你所做的就是通过http包含php文件。这将导致PHP执行PHP代码并返回结果。但是你的api php文件没有返回任何输出。
你可能想做的是通过url作为服务调用你的api。您的API应该作为服务器运行,接受请求并响应。
网上有很多关于如何做到这一点的教程,也有很多库和框架。
- 使用PHP创建RESTful API
- 用PHP编写REST服务器简介 REST API -一个简单的PHP教程 RESTful Webservices mit PHP
为了保持简单,你可以做另一件事。构建api并简单地调用提供所需输出的文件。例如:
api/random_password.php
<?php
$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
$pass = array(); //remember to declare $pass as an array
$alpha_length = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 8; $i++) {
$n = rand(0, $alpha_length);
$pass[] = $alphabet[$n];
}
echo json_encode(implode($pass)); //turn the array into a string and output it
本地:test_locaal.php
<?php
$randomPassword = file_get_contents("http://***api.new*******.nl/api/random_password.php");
print_r($randomPassword);