1

Send the POST request to some website API

function httpPost($url,$params)
{


$ch = curl_init($url); 
$contents= json_encode($params);
$access_token='**************************';
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Token ' . $access_token,
  'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $contents);

$output=curl_exec($ch); 
curl_close($ch);
return $output;

}

$params = file_get_contents('./upload.json');
echo httpPost("https://xxxxx.com/api/v2/review/",$params);

but it can't send the POST request to the server.

 Remote Address:[::1]:80
 Request URL:http://localhost/publons/submit.php
 Request Method:GET
 Status Code:500 Internal Server Error

Why it use the GET, it should be POST ????

shadrx
  • 941
  • 1
  • 8
  • 22
AntiGMO
  • 1,535
  • 5
  • 23
  • 38
  • So the receiving server is seeing your request as a GET? – Jonathan May 04 '15 at 02:52
  • Aren't you just looking at the error generated by your script, not the one you're posting to using curl? Unless your example should read `echo httpPost("http://localhost/publons/submit.php",$params);` Not having the curl extension installed on your local server would indeed throw an error 500 – Capsule May 04 '15 at 02:56
  • Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Pedro Lobito May 04 '15 at 02:57
  • It seems fine, you could swap out `curl_setopt($ch, CURLOPT_POST, 1);` with `curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');` but really they do the same thing. As others have suggested. You can also try `echo curl_error($ch);` just before you close it to see if there were any errors. – Jonathan May 04 '15 at 03:02
  • @PedroLobito add error reporting, not shows any errors – AntiGMO May 04 '15 at 03:28
  • @Augwa Thanks, after add echo curl_error($ch), it shows SSL certificate problem: Invalid certificate chain. – AntiGMO May 04 '15 at 03:31
  • So the error 500 was in your script you were accessing using GET, not the one you were trying to POST to, hence the GET... – Capsule May 04 '15 at 03:41

1 Answers1

1

add this to not verify the SSL certificate. If it's a development server it's fine, if it's production though this should always be verified.

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Jonathan
  • 2,778
  • 13
  • 23