Reputation: 3078
I have this PHP code that makes a curl call:
$postData['api_user'] = 'username';
$postData['api_key'] = 'password!';
$ch = curl_init('https://api.example.com/send.json');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
I know that in order to make a curl call with json in bash I have to do this:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" http://hostname/resource
But I'm confused in how I can transfer all my php code to bash curl call, what I mean is for example what would be the equivalent of curl_setopt(), and to pass the credentials as an array like I did with http_build_query()
Upvotes: 2
Views: 3747
Reputation: 587
-d
is the curl
flag to send POST
data, but you'll have to format it as a JSON string:
curl -H "Content-Type: application/json" -d '{ "api_user": "username", "api_key": "password" }' https://api.example.com/send.json
And now with those extra options (look them up in the curl man page):
curl -X POST -k -H "Content-Type: application/json" -d '{ "api_user": "username", "api_key": "password" }' https://api.example.com/send.json
As a much friendlier alternative to curl
I recommend checking out httpie. Here's what your call would look like with httpie
:
http --verify=no https://api.example.com/send.json api_user=username api_key=password
Upvotes: 0
Reputation: 41747
Use cURL to POST your data as JSON.
curl -i \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-X POST --data '{"api_user": "username", "api_key":"password!"}' \
--insecure \
https://api.example.com/send.json
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
equals -k, --insecure
.
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData))
equals -X POST --data ...
CURLOPT_RETURNTRANSFER
... hmm, i don't know ,)
Upvotes: 3