Make API request with cURL PHP - php

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);

Related

How to curl via php [closed]

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.

Pass Params in Curl Request

I have try curl request with terminal it's working but when i convert that curl request into php code that one passing param not working.
Terminal curl request :
curl --insecure "https://www.zohoapis.in/phonebridge/v3/clicktodial" -X POST -d "clicktodialuri=$clicktodialurl&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=123456" -H "Authorization: Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf" -H "Content-Type: application/x-www-form-urlencoded"
Response :
{"message":"ASTPP Clicktodial functionality has been enabled","status":"success","code":"SUCCESS"}
PHP Code :
$zohouser = '6000';
$access_token = '1000.c3c1107b635f1f5b257d831677e077d2';
$cURL = "https://www.zohoapis.in/phonebridge/v3/clicktodial?clicktodialuri=$click_to_dial&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=$zohouser";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $cURL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"Authorization: Zoho-oauthtoken " . $access_token,
"Content-Type: application/x-www-form-urlencoded",
"cache-control: no-cache"
),
));
$response = json_decode(curl_exec($curl));
$err = curl_error($curl);
print_r($err);
curl_close($curl);
print_r($response);exit;
Not getting any response or error during run this php curl request.
Can you please help me how to pass string as param in php curl request.
$strURL= "https://www.zohoapis.in/phonebridge/v3/clicktodial";
$arrHeader= array(
'Authorization:Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf'
);
$params= array(
"clicktodialparam"=>"[{\"name\":\"fromnumber\",\"value\":\"555\"}]",
"authorizationparam"=>"{\"name\":\"X-Auth-Token\",\"value\":\"1000.aedb399e2389cfacef60f965af052cbf\"}",
"clicktodialuri" => "$click_to_dial",
"zohouser" => "123456"
);
$ch= curl_init();
curl_setopt_array($ch,array(
CURLOPT_URL =>$strURL,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $arrHeader,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 0,
CURLOPT_TIMEOUT => 0,
CURLOPT_POSTFIELDS => http_build_query($params)
));
$strResponse= curl_exec($ch);
print_r($strResponse);
echo curl_error($ch);
Referring to this post and using the linked tool we end up with the following code. Since I cannot test this myself I cannot guarantee my answer. It looks like you might be missing the curl post option "curl_setopt($ch, CURLOPT_POST, 1);" which you can use in lieu of the option you used "CURLOPT_CUSTOMREQUEST => "POST"".
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.zohoapis.in/phonebridge/v3/clicktodial');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "clicktodialuri=$clicktodialurl&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=123456");
$headers = array();
$headers[] = 'Authorization: Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);

PHP cURL POST dataset

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";
?>

cURL PHP Headers and Data

I am attempting to recreate a cURL request that looks like this:
curl -X "POST" "https://urlhere.com" \
-H "authorization: TOKEN" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-d $'{
"subscriptionid": "",
"templateid": "",
"to": [
""
],
"subject": "",
"data": {
"foo": 123,
"bar": 123
}
}'
Can anyone help me figure out how to create this in PHP? I currently have:
curl_setopt($cURL,CURLOPT_URL, $url);
curl_setopt($cURL,CURLOPT_POST, 1);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array('authorization: '. $token));
curl_setopt($cURL, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($cURL, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($cURL,CURLOPT_POSTFIELDS, $json);
curl_setopt($cURL, CURLINFO_HEADER_OUT, true);
And of course my init, exec, and close statements.
But I receive back an error 401. Unauthorized.
According to your given curl this is the code which i generated from postman
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://urlhere.com/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\n \"subscriptionid\": \"\",\n \"templateid\": \"\",\n \"to\": [\n \"\"\n ],\n \"subject\": \"\",\n \"data\": {\n \"foo\": 123,\n \"bar\": 123\n }\n}",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"authorization: TOKEN",
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

PHP cURL, POST JSON getting error in php curl

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"firstname":"Mike","lastname":"Doel","customer_id":"12345","email":"test_api_user#gmail.com.com"}' -u API-key: APIURL(http://)
above statement is running well in command but i am unable to achive the same by php code below is my code
$url="https://apiurl";
$data=array("firstname"=>"Mike","lastname"=>"Doel","customer_id"=>"12345","email"=>"test_api_user#gmail.com");
$data_json=json_encode($data);
//Curl code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',"Accept: application/json","api-key"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
In CURL, for api key you need to pass username:password , if you going to access in php code
$url="https://apiurl";
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => 'username:password',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 300000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"content-type: application/json"
),
CURLOPT_POSTFIELDS => $postfields,
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
return $response;
Try it once..hopefully it will work for you.
This:
curl_setopt($ch, CURLOPT_HTTPHEADER, array([..snip..], "api-key"));
^^^^^
and
curl [..snip..] -u api-key
^^^^^^^^^^
are NOT equivalent. -u specifies HTTP Basic authentication, with username:password. Your setopt is just stuffing that username as the NAME of an http header, which is not how basic auth credentials show up
You should have
curl_setopt($ch, CURLOPT_USERNAME, "api-key");
instead.

Categories