1

i have written code for Post request to REST API, but it's sending GET request. I'm printing request method at the end of the code. Please help me to solve this issue. and i'm working in localhost to get the response. And the

$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;
Sri P
  • 275
  • 1
  • 3
  • 13
  • 1
    Possible duplicate of [PHP + curl, HTTP POST sample code?](http://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code) – Kevin Stich Dec 13 '16 at 05:55

1 Answers1

0

This simple Post request

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

Before php code, try it in POSTMAN , if you got output, Postman itselt you can generate PHP code.

Alagesan
  • 349
  • 1
  • 11