簡體   English   中英

如何使用 php 中的 get 方法發送數據

[英]How can I send data using get method in php

我想使用 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";
}

?>

我們應該在GET上設置CURLOPT_CUSTOMREQUEST

    $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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM