Php 无卷曲HTTP请求(返回数组)
我是PHP新手,我正在尝试访问我的另一个网站的文件。因此,在我的网络上,我试图发送一个如下的POST请求:Php 无卷曲HTTP请求(返回数组),php,Php,我是PHP新手,我正在尝试访问我的另一个网站的文件。因此,在我的网络上,我试图发送一个如下的POST请求: <?php $url = 'http://localhost/modul_cms/admin/api.php'; //Web #2 $data = array( "Action" => "getNewestRecipe", "Secret" => "61cbe6797d18a2772176b0ce73c580d95f79500d77e45ef81003
<?php
$url = 'http://localhost/modul_cms/admin/api.php'; //Web #2
$data = array(
"Action" => "getNewestRecipe",
"Secret" => "61cbe6797d18a2772176b0ce73c580d95f79500d77e45ef810035bc738aef99c3e13568993f735eeb0d3c9e73b22986c57da60a0b2d6413c5dc32b764cc5897a",
"User" => "joomla localhost",
);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
if($result === FALSE){
echo "Not working connection :(";
}else{
echo "HOORAY!";
var_dump($result);
}
在web上#1您发送的是大写的“Secret”、“User”、“Action”,但在web上#2您访问的是$\u POST['Secret']
(小写)。因此,您的代码永远无法调用getNewestRecipe()
或error()
调用,因此没有内容/空白页,也没有错误
此外,还需要以某种方式输出函数返回的数组。
数组不能简单地echo
d,因此需要序列化它。我建议使用:echo json_encode(getNewestRecipe())代码>看起来像是web#2上发生了错误,它生成了一个空白页,因为错误报告被禁用。或者POST数据发送不正确,您甚至没有进入开关。看起来在这种情况下没有输出。可能是因为您发送了“Secret”(大写)并选中了“Secret”?我甚至不知道如何测试此变体,但当我在web上的数组中出错时,它会将我的错误返回到web 1(错误如:“预期”、“不”;)我实际上在想,如果使用file\u get\u contents
可以只获取数组,因为整个web#2只是没有echo的php或其他东西,它只是这个带返回的开关。我不知道只包含是否很安全。它应该是最终版本中的某种自定义API。web#1将是某人的web我不知道把它包括进去是否好。
<?php
$action = isset( $_POST["action"] ) ? $_POST["action"] : "";
$secret = isset( $_POST["secret"] ) ? $_POST["secret"] : "";
$user = isset( $_POST["user"] ) ? $_POST["user"] : "";
if(!empty($secret)){
if(!empty($user)){
switch($action){
case 'getNewestRecipe':
getNewestRecipe();
break;
case '':
error();
break;
default:
error();
break;
}
}
}
/* *************** FUNCTIONS ************* */
function getNewestRecipe(){
return array("msg" => "Here is your message!");
}