php - API send email and Password using CURL
I have written some code into a plugin for Wordpress that when the User hits the submit button their information is stored in the WP database and is also sent to a third party website with their email and password using JSON protocalls.
this is the code I use (the "$bemail" and $password are referring to earlier parts of the code in the same file):
$url = "https://api.example.com/v1/logins";
$data = array(
'email' => $_POST['$bemail'],
'password' => $_POST['$password'],
);
$content = json_encode($data);
$curl = curl_init($url);
$apikey = "xxxx";
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'example.com-Api-Key: ' . $apikey,
'Accept: application/json',
));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
I get an error with a response from the 3rd party website with something like:
"error: email and password cannot be blank"
which tells me that the email and password is not being sent.
what am I missing here?
Thank you.
Answer
Solution:
To tell the receiving server to expect JSON, use the
Content-Type
header instead ofAccept
:Answer
Solution:
The first thing I would suggest is to just use our PHP API client which you can find here: https://github.com/sproutvideo/sproutvideo-php
The documentation for creating a login is here: https://github.com/sproutvideo/sproutvideo-php#create_login
You can also see what is being received on the server side by logging into your account and going to http://sproutvideo.com/api_history
If you have any further questions about getting set up with the API, please reach out to our customer support directly!