我将参数数组传递给api并尝试以json格式返回相同的数组元素
我创建了非常简单的2页
1。 用于发送带有api的数组
<?php
$array['Phone'] = 123456;
$array['name'] = 'name1';
$array['email'] = 'testemail';
$array['password'] = 'password';
$array['api_key'] = $api_key;
$array['referral_code'] = 'kk';
$url = "http://getsjobs.esy.es/registerapi.php?Array[]=$array";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 4);
$json = curl_exec($ch);
if(!$json) {
echo curl_error($ch);
}
curl_close($ch);
$data = json_decode($json,true);
foreach ($data as $item) {
echo $data;
}
?>php
和
尝试返回与JSON格式相同的数组(用于测试)
<?PHP
if(isset($_GET['Array[]'])) {
$_Phone= $array['Phone'];
$_name = $array['name'];
$_email = $array['email'];
$_password = $array['password'];
$api_key = md5(uniqid(rand(), true));
$_referral_code = $array['referral_code'];
$posts = $_GET['Array[]'];
header('Content-type: application/json');
}
?>
但SomeHow Json数据无法从api获取数组。我试过通过以下示例仔细创建代码,但我觉得这里缺少一些东西。
只需要一个提示可能是阵列没有通过或接收的原因?任何帮助都会被欣赏
答案 0 :(得分:0)
传递数据的方式不正确,PHP可能会在此行上抛出一个Array to String转换错误
$url = "http://getsjobs.esy.es/registerapi.php?Array[]=$array";
你需要这样做
<?php
$api_key = '';
$params = [
'Array[Phone]' => 123456,
'Array[name]' => 'name1',
'Array[email]' => 'testemail',
'Array[password]' => 'password',
'Array[api_key]' => $api_key,
'Array[referral_code]' => 'kk',
];
$url = "http://localhost/test.php?".http_build_query($params);
// echo $url;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 4);
$json = curl_exec($ch);
if(!$json) {
echo curl_error($ch);
}
curl_close($ch);
$data = json_decode($json,true);
foreach ($data as $item) {
echo $item."<br/>\n";
}
要检索数据:
<?php
if(isset($_GET['Array'])) {
$array = $_GET['Array'];
$_Phone= $array['Phone'];
$_name = $array['name'];
$_email = $array['email'];
$_password = $array['password'];
$api_key = md5(uniqid(rand(), true));
$_referral_code = $array['referral_code'];
echo json_encode($array);
}