I am very new to the curl and i need to send the POST request to the following url:
curl -X POST https://connect.stripe.com/oauth/token \
-d client_secret=Secret key \
-d code=AUTHORIZATION_CODE \
-d grant_type=authorization_code
in the response i will be receiving the access token from the stripe.
please let me know how can create the function in php to do the same using the php - cURL.
Thanks for your help.
Stripe would probably advise to use their libraries but this is a curl tool I've been playing with here this morning; I prefer curl over libraries that wrap around it any day:
$post = 'client_secret='.$system['stipe']['client_secret'].'&grant_type=authorization_code&code='.$_GET['code'];
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $system['stipe']['token_url']);
curl_setopt($ch,CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
You just need to wrap it up as a function and json_decode the values returned.
This works for me whereas the code in the docs / gist didn't as they were lacking the false flag on the SSL verify peer which in my dev setup here made things prang.
Related
Here is my problem. I'm running a service on a remote machine working perfectly. The way to get the results from the machine is via api.
curl -X GET http://ip:777/api \
-d "r=request"
It works perfectly on the terminal. Moreover, it works perfectly, if the request query is short. But, it turns into a huge problem once, it passes some length(1800-2000 characters and I need 7k-8k chars).
However, I can't "transliterate" the curl code into PHP. If there is anyone with any idea how to do it please show me the way. As much as, I'm aware, this is a curl GET method with REQUEST BODY.
$long_query = "r=" . $request;
// set the api
curl_setopt($ch, CURLOPT_URL, 'http://ip:777/api');
// i want to get the return
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 2min+ timeout as to make sure that I get a result
curl_setopt($ch, CURLOPT_TIMEOUT, 140);
// Set request method to GET by 0'ing the POST method
curl_setopt($ch, CURLOPT_POST, 0);
// Set query data here with CURLOPT_POSTFIELDS
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($long_query));
$content = curl_exec($ch);
curl_close($ch);
echo $content;
What am I doing wrong in here? If someone knows, please explain as if you are teaching a year old. Thanks in advance!
I think the following doc would help you understand how GET method works. This is from RFC 7231
A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.
For more details, please refer to this answer.
Alright, here we go with the proper answer.
on terminal,
curl -X GET http://ip:777/api \
-d "r=request"
works perfectly. However, the problem with converting that to php curl is quite troublesome while very easy at the same time.
I've read through every stack problem regarding this and no-one has provided a clear answer to the problem. I'm not sure the reason behind it but as a generous person I'll give out the code so that anyone in the future facing this rare problem will solve it out easily.
Long story short,
curl -X GET -d is the same as curl -X POST -H "X-HTTP-Method-Override: GET".
The actual request is POST but THE SERVER will consider it as a GET. This way you won't face the LONG URI problem.
$long_query = "r=" . $request;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"ip:777/api");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $long_query); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
'X-HTTP-Method-Override: GET',
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
curl_setopt($ch, CURLOPT_TIMEOUT, 140);
curl_close ($ch);
var_dump($server_output);
I've set the timeout to 140 as the query is long and it takes a bit of time for the server to go through it and respond (in my case its a json). Nevertheless, I've added var_dump so that anyone who uses it in the future might see if its a serialized array or whatever.
Good luck!
I am trying to translate the below cURL to php cURL:
$ curl -X POST https://tartan.plaid.com/exchange_token \
-d client_id="$plaid_client_id" \
-d secret="$plaid_secret" \
-d public_token="$public_token_from_plaid_link_module"
using this code:
$data = array(
"cliend_id"=>"test_id",
"secret"=>"test_secret",
"public_token"=>"test,fidelity,connected");
$string = http_build_query($data);
echo $string;
//initialize session
$ch=curl_init("https://tartan.plaid.com/exchange_token");
//set options
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute session
$exchangeToken = curl_exec($ch);
echo $exchangeToken;
//close session
curl_close($ch);
and I am getting this response:
cliend_id=test_id&secret=test_secret&public_token=test%2Cfidelity%2Cconnected{ "code": 1100, "message": "client_id missing", "resolve": "Include your Client ID so we know who you are." }
I am not sure what is wrong with my format that is keeping plaid from recognizing the client_id portion of the post. For further reference, I have more detail below.
The below is taken from the plaid site that can be found by searching "plaid api quickstart":
Reference
/exchange_token Endpoint
The /exchange_token endpoint is available in both the tartan and production environments.
Method Endpoint Required Parameters Optional Parameters
POST /exchange_token client_id, secret, public_token account_id
The /exchange_token endpoint has already been integrated into the plaid-node, plaid-go, plaid-ruby, and plaid-python client libraries. Support for plaid-java is coming soon.
If you are working with a library that does not yet support the /exchange_token endpoint you can simply make a standard HTTP request:
$ curl -X POST https://tartan.plaid.com/exchange_token \
-d client_id="$plaid_client_id" \
-d secret="$plaid_secret" \
-d public_token="$public_token_from_plaid_link_module"
For a valid request, the API will return a JSON response similar to:
{
"access_token": "foobar_plaid_access_token"
}
The problem is you are sending cliend_id but the server expects client_id:
$data = array(
"client_id"=>"test_id", // Use client_id instead of cliend_id
"secret"=>"test_secret",
"public_token"=>"test,fidelity,connected");
I'm trying to use the Facebook Marketing API as detailed in this tutorial.
However I'm stumped with how to translate this suggested cURL command line request into it's PHP equivalent:
curl -G \
-d 'access_token=<ACCESS_TOKEN>' \
https://graph.facebook.com/v2.5/<LEAD_ID>
I would normally do this:
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$access_token);
$result = curl_exec($ch);
But that produces an 'Unsupported post request' error when attempting to run it. I think I am misunderstanding what the '-G' means in the command line version?
From man curl:
-G, --get
When used, this option will make all data specified with -d, --data, --data-binary or --data-urlencode to be used in an HTTP GET request instead of the POST request that otherwise would be used. The data will be appended to the URL with a '?' separator.
There's no cURL option flag in PHP that directly corresponds to this. You can use
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
or
curl_setopt($ch, CURLOPT_HTTPGET, 'GET');
but this is hardly necessary:
CURLOPT_HTTPGET
TRUE to reset the HTTP request method to GET. Since GET is the default, this is only necessary if the request method has been changed.
You will have to specify the request parameters differently: instead of setting CURLOPT_POSTFIELDS, append them as a query string to the URL (using urlencode or the equivalent curl_escape if needed):
curl_setopt( $ch, CURLOPT_URL, $url . '?accesstoken='.urlencode('<ACCESS_TOKEN>');
I have a curl request to get some information about database and stuff, but since some user need to query many bases and most of them are not familiar with any kind of shell,
we are trying to build a small page to e"xecute the query and be more user friendly
we got pretty much everything working (form and stuff) but we got really figure how to build the uery in php
here is the original working curl request we made:
curl -s - X POST -H 'Content-type:application/json' -H
'Accept:application/json' -d '{"param1":"value1","param2":"value2"} -k
https:myurl -u user:password
and here is the code with the request we are trying to build:
<?php
$data = array('param1' => 'value1',
'param2' => 'value2'
);
$ch = curl_init();
$url = "https://myurl";
curl_setopt($ch, curlopt_url, $url);
curl_setopt($ch,curlopt_userpwd, "user:password" );
curl_setopt($ch,curlopt_post, 1);
curl_setopt($ch,curlopt_postfuelds, $data);
curl_setopt($ch,curlopt_returntransfer, true);
curl_setopt($ch,curlopt_ssl_verifypeer, false);
curl_setopt($ch,curlopt_httpheader,
array ("Content-type:application/json"));
$output = curl_exec($ch);
curl_close($ch);
I cannot really figure whats wrong? can you help me?
I rather not use shell_exec with the curl inside because the server hosting is on windows, and we don't have curl.exe available
thanks
You have a typo: curlopt_postfuelds should be curlopt_postfields
Evening folks,
I am attempting to load data from a .json file into parse.com backend. I can get the rest api to load manually added json as per the example provided from Parse.com from the terminal on my Mac:
curl -X POST \
-H "X-Parse-Application-Id: removed" \
-H "X-Parse-REST-API-Key: removed" \
-H "Content-Type: application/json" \
-d '{"score":1337,"playerName":"Sean Plott","cheatMode":false}' \
https://api.parse.com/1/classes/Test
This works fine.
How can I read a saved .Json file with only these three parameters(Json is validated fine) and send the data to my Parse backend?
"score":1337,"playerName":"Sean Plott","cheatMode":false
I believe I have the option to use PHP/Jquery etc. however I have not used any of these languages extensively and a quick code example would be very much appreciated.
Thanks,
Gerard
Here is pure PHP solution:
$url = 'https://api.parse.com/1/classes/Test';
$fields_string = file_get_contents('file.json');
//open connection
$ch = curl_init();
//set the url, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
/* End of first request */