我已经编写了针对REST API的Post请求的代码,但是它正在发送GET请求

时间:2016-12-13 05:42:05

标签: php rest api curl

我已经为REST API编写了Post请求的代码,但它正在发送GET请求。我在代码末尾打印请求方法。请帮我解决这个问题。我正在localhost工作以获得响应。

$credentials = "xxxx:yyyy";
$url = "http://api.utrust.in/get/token"; 
$page = "/get/token"; 
$headers=[ 
    'POST ".$page." HTTP/1.1',
    'Content-Type: application/x-www-form-urlencoded',
    'Content-Length:0',
    'Authorization: Basic ' . base64_encode($credentials),
    'Connection:keep-Alive',
    'Host:api.utrust.in',
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, array());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers); 
$data = curl_exec($ch); 
curl_close($ch);
echo $data;
print_r(get_headers($url));
$CurrentURL1="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['REQUEST_METHOD'].$_SERVER["REMOTE_ADDR"];;
echo  $CurrentURL1;

2 个答案:

答案 0 :(得分:0)

尝试更正此行

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

答案 1 :(得分:0)

这个简单的帖子请求

$ch = curl_init();  
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_POST, count($postData));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);    

$output=curl_exec($ch);

curl_close($ch);

在PHP代码之前,在POSTMAN中尝试一下,如果你有输出,Postman itselt你可以生成PHP代码。