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

问题描述 投票:0回答:1

我想使用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;
php api curl
1个回答
0
投票

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 choiche is donald";
}else{
   echo "Your choiche is other";
}

?>
© www.soinside.com 2019 - 2024. All rights reserved.