我的工作,其中需要一個JSON API類似刪除反斜槓,並添加協議
{
"images":[
{"id":1,"name":"test1","url":"image url"},
{"id":2,"name":"test2","url":"image url"}
]
}
一個項目,但我得到不同的URL格式和我寫的代碼是什麼
<?php
header('Content-Type: application/json');
function getJson(){
$array = array('images' => array(array('id' => 1, 'name' => 'test1', 'url' => $_SERVER['HTTP_HOST'].DIRECTORY_SEPARATOR.'test'.DIRECTORY_SEPARATOR.'test1.jpg'),
array('id' => 2, 'name' => 'test2', 'url' => $_SERVER['HTTP_HOST'].DIRECTORY_SEPARATOR.'test'.DIRECTORY_SEPARATOR.'test2.jpg')));
return $array;
}
$typeArray=null;
if(isset($_GET['request'])){
if($_GET['request']=='images'){
$typeArray=getJson();
}else{
$typeArray = array(array('status' => 'failed', 'message' => 'Invalid request url'));
}
}else{
$typeArray = array(array('status' => 'failed', 'message' => 'Invalid request url'));
}
echo json_encode($typeArray);
echo $_SERVER['SERVER_PROTOCOL'];
?>
但是我卻越來越
{"images":[{"id":1,"name":"test1","url":"picd.flynox.com\/test\/test1.jpg"},{"id":2,"name":"test2","url":"picd.flynox.com\/test\/test2.jpg"}]}HTTP/1.1
我推,這會更改測試服務器http://picd.flynox.com/api/test/images
爲什麼'echo $ _SERVER ['SERVER_PROTOCOL'];'到底? –
@ DurgeshChaudhary,我需要URL像「http:// picd.flynox.com」,但我通過使用$ _SERVER ['HTTP_HOST']得到「picd.flynox.com」。我的意思是,如何添加像http協議://或https://但我不能硬編碼。 –