Jump to content

using curl


funbinod

Recommended Posts

<!-- first of all completely sorry if this is not the right forum to ask this. but i've no idea which is the right forum for this --->

hello all!

i was learning how to use back4app parse on my website. while going through documentation on parseplatform.org, i found a sample block as below..

curl -X POST \
  -H "X-Parse-Application-Id: APPLICATION_ID" \
  -H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
  -H "X-Parse-Revocable-Session: 1" \
  -H "Content-Type: application/json" \
  -d '{"username":"cooldude6","password":"p_n7!-e8","phone":"415-392-0202"}' \
  https://parseapi.back4app.com/parse/users

but i could not understand how to implement this on my php code.

this might be because i am completely unfamiliar to curl.

UPDATE: while searching the web for more about uses of curl, i found something else that doesn't match the above format given by parseplatform.org. this blew my head more.

please someone help me understand all these things.

Edited by funbinod
Link to comment
Share on other sites

That's the curl command line tool. If you want to use their service with PHP, the easiest way is to use their PHP SDK. Most likely they have clear documentation in their tutorials for the PHP SDK.

If you want to use PHP's implementation of curl, you'll have to learn to translate the command line language to PHP. All of this can be done by setting options with the curl_setopt() method. The manual page has a list of all the possible options.
In the example you posted, it starts by indicating that the method is POST. In PHP the option for that is CURLOPT_POST. The following four items are HTTP headers. You can add  headers in PHP using the CURLOPT_HTTPHEADER option. Following that is the request  body, this would go in th

<?php
// Information needed for the request
$api_key = 'XXXXXXXXXXXXXXXX';
$data = [
  'username' => 'cooldude6',
  'password' => 'p_n7!-e8',
  'phone' => '415-392-0202'
];

// Send a cURL request
$request = curl_init('http://parseapi.back4app.com/parse/users');
curl_setopt(CURLOPT_POST, true); // This is a POST request
curl_setopt(CURLOPT_HTTPHEADER, [
  'X-Parse-Application-Id: APPLICATION_ID',
  'X-Parse-REST-API-Key: ' . $api_key,
  'X-Parse-Revocable-Session: 1',
  'Content-Type: application/json'
]);
curl_setopt(CURLOPT_POSTFIELDS, json_encode($data));
curl_exec();

By default, PHP prints the curl response straight to the browser. If you want to store the result in a variable instead, you should read the PHP manual on how to use the CURLOPT_RETURNTRANSFER option.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...