我有这个 javascript 数组
[
"spiderman",
"xmen",
"barbie",
"avengers"
]
这是我在打开 http://localhost:8080 时在屏幕上看到的内容
如果我决定在 Controller 中使用此代码在 laravel 中显示它:
public function showReccPosts(){
$name='alice';
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, [
CURLOPT_URL => "http://localhost:8080/?name=$name",
CURLOPT_PROXY => '',
]);
// Send the request & save response to $resp
$results = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
return view('reccomended.index',compact('results'));
}
}
在 Blade 文件中:
{{$结果
我得到:
[
"spiderman",
"xmen",
"barbie",
"avengers"
]1
数组末尾的 1 是怎么回事? 它会阻止我在阵列上执行功能吗? 顺便问一下,我什至可以使用那种类型的数组(从 js 到 php)吗?
最佳答案
您的 CURL 请求没有将内容返回到您的 PHP 代码,可能只是显示它。然后,您将从 CURL 返回(1
),表明它已成功,然后显示它。通过设置 CURLOPT_RETURNTRANSFER
...
curl_setopt_array($curl, [
CURLOPT_URL => "http://localhost:8080/?name=$name",
CURLOPT_PROXY => '',
CURLOPT_RETURNTRANSFER => true
]);
关于javascript - 从 javascript 更改为 php/laravel 时,数组在后面加上 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56196609/