首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用php中的get方法发送数据?

如何使用php中的get方法发送数据?
EN

Stack Overflow用户
提问于 2020-04-08 11:29:39
回答 2查看 83关注 0票数 0

我想使用GET方法发送数据,但是这段代码不起作用,而且我有一个错误提示:

请求的资源不支持http方法'POST'

代码语言:javascript
运行
复制
$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;

我的数据:

代码语言:javascript
运行
复制
[
    {
        "id": 258,
        "value": 10,
        "Price": 560,
    },
    {
        "id": 259,
        "value": 5,
        "Price": 720,
    }
]
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-09 09:46:10

我们应该将CURLOPT_CUSTOMREQUEST设置为GET

代码语言:javascript
运行
复制
    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => "URL",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
      CURLOPT_POSTFIELDS =>$content,
      CURLOPT_HTTPHEADER => array(
        "Content-Type: application/json"
      ),
    ));
    $response = curl_exec($curl);
    curl_close($curl);
票数 0
EN

Stack Overflow用户

发布于 2020-04-08 12:04:01

GET方法使用URL发送数据示例:

代码语言:javascript
运行
复制
http://site/mypage.php?variable=value
http://site/index.php?show=donald

在PHP代码中,您必须使用这种方式读取URL中的变量

代码语言:javascript
运行
复制
<?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";
}

?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61099615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档