Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have to execute the following via php script
curl --location --request POST 'https://api.mydomain.in/api/comm/wa/send/text' \
--header 'Content-Type: application/json' \
--header 'token: xyz123456' \
--data-raw '{
"phone": "8822992929",
"ID": 26,
"text": "Dear Customer. Thank you for your purchase. "
}'
How do I do this via php curl exec. I do not see how I can pass the data in this
You should try to do something from your side and mention it on the question. Anyways here's the solution for your problem
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.mydomain.in/api/comm/wa/send/text',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"phone": "8822992929",
"ID": 26,
"text": "Dear Customer. Thank you for your purchase. "
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'token: xyz123456'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
$post = array( "phone" => "8822992929",
"ID" => 26,
"text" => "Dear Customer. Thank you for your purchase.");
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
/*$headers = array(
'Content-Length: 0',
'Content-Type: application/json; charset=us-ascii',
'token: xyz123456',
);*/
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_USERNAME, $username);
//curl_setopt($ch, CURLOPT_USERPWD, $password);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
It's a version I use for my php. You need to edit the $headers array to use json and also send the token. It's hard to tell, but I think you need data-raw as "POST-like" array, which then goes to the CURLOPT_POSTFIELDS, $post part. Username, password, are optional, you may need to change authentication too.
Related
I am trying to connect to an API, which should be done with cURL.
This is what the documentation is telling me to send (with my own data though, this is just and example).
curl --request POST \
--url https://api.reepay.com/v1/subscription \
--header 'Accept: application/json' \
-u 'priv_11111111111111111111111111111111:' \
--header 'Content-Type: application/json' \
--data '{"plan":"plan-AAAAA",
"handle": "subscription-101",
"create_customer": {
"handle": "customer-007",
"email": "joe#example.com"
},
"signup_method":"link"}'
What I have tried is this, but I get and error:
$postdata = array();
$postdata['plan'] = 'plan-AAAAA';
$postdata['handle'] = 'subscription-101';
$postdata['create_customer'] = ["handle" => "customer-007", "email" => "joe#example.com"];
$postdata['signup_method'] = 'link';
$cc = curl_init();
curl_setopt($cc,CURLOPT_POST,1);
curl_setopt($cc,CURLOPT_RETURNTRANSFER,1);
curl_setopt($cc,CURLOPT_URL, "https://api.reepay.com/v1/subscription");
curl_setopt($cc,CURLOPT_POSTFIELDS, $postdata);
$result = curl_exec($cc);
echo $result;
This is the error I get:
{"error":"Unsupported Media Type","path":"/v1/subscription","timestamp":"2022-10-22T11:42:11.733+00:00","http_status":415,"http_reason":"Unsupported Media Type"}
Can anyone help me make the correct request?
The example says, that application/json is accepted, but you are posting application/x-www-form-urlencoded. You'll need to json_encode the postdata and put it into the body + set the appropriate content-type.
To be nice, also set 'Content-Length'...
$json_data = json_encode($postdata);
curl_setopt($cc, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($cc, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: '.strlen($json_data)
]);
Based on the error you get, I guess you need to set the content-type header as JSON.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.reepay.com/v1/subscription',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"plan": "plan-AAAAA",
"handle": "subscription-101",
"create_customer": {
"handle": "customer-007",
"email": "joe#example.com"
},
"signup_method": "link"
}',
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
This should work:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.reepay.com/v1/subscription');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
]);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'priv_11111111111111111111111111111111:');
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"plan":"plan-AAAAA",\n "handle": "subscription-101",\n "create_customer": {\n "handle": "customer-007",\n "email": "joe#example.com"\n },\n "signup_method":"link"}');
$response = curl_exec($ch);
curl_close($ch);
I've been given the following example in order to post data to a API url
curl --request POST \
--url https://apiurl \
--header 'auth-token: {{token}}' \
--header 'content-type: application/json' \
--data '{
"user": {
"email": "my#email.com",
"name": "James",
"tel": "0000000"
}
}'
I got my cURL working using the following code but I need to post the user parameters as above like email, name, tel etc.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array('Cache-Control: no-cache', 'auth-token: '.$token)
));
$response = curl_exec($curl);
curl_close($curl);
How can I post the fields as the example states using my code?
This was already answered here: How to POST JSON Data With PHP cURL?
You just need to add like the following:
$payload = json_encode(['user'=> ['email'=>'test#example.com','name'=>'Joe','tel'=>'123e332']] );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
i use in this way:
<?php
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_RETURNTRANSFER,1);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, array(
'data' => '{
"user": {
"email": "my#email.com",
"name": "James",
"tel": "0000000"
}
}'
));
$dados = curl_exec($handle);
curl_close($handle);
echo "$dados";
?>
hello i'm using code used by other people who supposedly have gotten it to work and have gotten their token information retrieved. The code is as follows:
$ch = curl_init();
$clientId = "myclientid";
$secret = "mysecret";
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLVERSION , 6); //NEW ADDITION
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$result = curl_exec($ch);
if(empty($result))die("Error: No response.");
else
{
$json = json_decode($result);
print_r($json->access_token);
}
curl_close($ch); //THIS CODE IS NOW WORKING!
I retrieved this code from Paypal API with PHP and cURL and have seen it implemented in some others peoples code so i assume that it works. However i'm only receiving no response even though i am supplying the correct client id and secret (maybe a recent update has broken this code?).
The guide provided by Paypal on getting the access token is found here-> https://developer.paypal.com/docs/integration/direct/make-your-first-call/
however it demonstrates the solution in cURL and not through the PHP cURL extension so its alittle cryptic to me. Any help?
Well it seems that it is now a requirement to declare what type of SSL Version to use, thus the code above will work when curl_setopt($ch, CURLOPT_SSLVERSION , 6); //tlsv1.2 is inserted.
Just tested the API and I can confirm this code is working :
(No SSL options)
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sandbox.paypal.com/v1/oauth2/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_USERPWD => $PAYPAL_CLIENT_ID.":".$PAYPAL_SECRET,
CURLOPT_POSTFIELDS => "grant_type=client_credentials",
CURLOPT_HTTPHEADER => array(
"Accept: application/json",
"Accept-Language: en_US"
),
));
$result= curl_exec($curl);
$array=json_decode($result, true);
$token=$array['access_token'];
echo "<pre>";
print_r($array);
I want to fetch content from my Atlassian with username and password.
The URL typically looks like:
http://my-own-site.atlassian.net/wiki/pages/viewpage.action?spaceKey=TO&title=Any-Wiki-Title
Is it possible to use PHP CURL to fetch content from this page?
So far I am only getting 401 auth reqd error.
I have looked through Stackoverflow and all I am getting is how to access basic atlassian.com and bitbucket.org pages.
With this code in php, you can create Confluence Pages:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:8090/rest/api/content/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"type\":\"page\",\"title\":\"**inserttitle**\",\"space\":{\"key\":\"**insertspace**\"},\"ancestors\":[{\"type\":\"page\",\"id\":**insertancestor**}],\"body\":{\"storage\":{\"value\":\"<p>This is a new page</p>\",\"representation\":\"storage\"}}}");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "**insertusername**" . ":" . "**insertpassword**");
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
?>
And with this code, you can get content from Confluence:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:8090/rest/api/content/**insertid**");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, "**insertusername**" . ":" . "**insertpassword**");
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
?>
echo $result;
Modify the parameter. I marked the words with insert*
Yes, it is certainly possible to access Atlassian products using PHP and cURL. I do it all the time to create/modify Jira issues
You will have to find/write a library (or set of libraries) which will allow you to access REST API calls. In my case, I wrote a base REST library which can then be inherited to create Jira, Confluence, any other REST service libraries
Search the Atlassian documentation site to find the REST API for the product you're using (Confluence in your case I would guess)
Don't forget that the REST API uses GET, POST, PUT and DELETE methods so your library will need to handle all of these
With regards to your error, I *think* your login will need to be allowed access to the API calls
To retrieve any existing content properties for a piece of content, use url as https://your-domain.atlassian.net/wiki/rest/api/content/{content_ID}
$curl = curl_init();
$post = array(
"id" => "{content_ID}",
"type" => "{content_ID}", //Ex. page
"title" => "{content_Title}",
"space" => ["key" => "{spaces_key}"],
"body" => ["storage" => ["value" => "<p>Here comes the other text</p>", "representation" => "storage"]],
"version" => ["number" => 15]
);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://your-domain.atlassian.net/wiki/rest/api/content/{content_ID}?expand=metadata.properties.myprop,space,body.view,version,container",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 300,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => json_encode($post),
CURLOPT_HTTPHEADER => array(
"authorization: Basic {base64 of (username:password)}",
"content-type: application/json",
'Accept: application/json'
),
));
$result = curl_exec($curl);
curl_close($curl);
I have been writing a RESTful API for my Laravel application.
I can submit data using cURL in Terminal with the following line;
curl -i --user admin#admin.com:qwertyuiop -d "data=somedata" https://www.xxxxxxxxxxxxx.co.uk/app/api/v1/clients
but when i try and do the same in cURL using PHP it gives me a 403 forbidden error.
$url = "https://www.xxxxxxxxxx.co.uk/app/api/v1/clients";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, "admin#admin.com:qwertyuiop");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "data=mydata");
$output = curl_exec($ch);
curl_close($ch);
print_r($output);
Any ideas what i'm missing from my PHP cURL script?
Thanks
It's been almost 6 years since this question asked. But I'm sure this answer will solve your and probably another problem with this. This code also solved my problem.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.com",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
// Set Here Your Requested Headers
'Content-Type: application/json',
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
dd($response); //this line is my custom code
}
As explained in this amazing website, where I found this code about
Laravel POST using cURL