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 ????

7
  • So the receiving server is seeing your request as a GET? Commented May 4, 2015 at 2: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 Commented May 4, 2015 at 2:56
  • Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, to see if it yields anything. Commented May 4, 2015 at 2: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. Commented May 4, 2015 at 3:02
  • @PedroLobito add error reporting, not shows any errors Commented May 4, 2015 at 3:28

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.