php - 如何在 php 中使用 get 方法发送数据
问题描述
我想使用 GET 方法发送数据,但此代码不起作用,并且出现错误消息:
请求的资源不支持 http 方法 'POST'
$url = "http://.....URL";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_HTTPGET, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
echo $response;
我的数据:
[
{
"id": 258,
"value": 10,
"Price": 560,
},
{
"id": 259,
"value": 5,
"Price": 720,
}
]
解决方案
GET 方法使用 URL 发送数据示例:
http://site/mypage.php?variable=value
http://site/index.php?show=donald
在 PHP 代码中,您必须使用这种方式来读取 URL 中的变量
<?php
echo 'Show ' . htmlspecialchars($_GET["show"]) . '!';
$show = $_GET["show"]; //store the value of show inside the variable show
if ( $show == "donald"){ // compare the variable show if the content are the same or not
echo "Your choice is donald";
}else{
echo "Your choice is other";
}
?>
推荐阅读
- python - 云调度程序不接受我为计划作业创建的服务帐户
- regex - 在 Spark 数据框列中的特定字符串之后提取数字 - Scala
- c# - 从代码中执行 Sqlite3 命令行工具
- pandas - 大熊猫在 .json 文件中导出为 TZ 时间格式
- reactjs - Reducer Init State 中的两个布尔状态,一个返回 undefined
- java - 使用 split() 从文件中获取段落数组。"\n" 只按行而不是段落间隔
- windows - 我需要 Windows 中 REACT 的帮助
- reactjs - React 继承与组合模式
- css - 如何显示不同大小的元素
- python - 当我在书评应用程序上提交评论时无法调整类型“RowProxy”错误