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;
}
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";
?>
i have a problem when send request header to my restful, my restful checking request Authorize header, but when i send the header is missing.
My restful debug give me result NULL
i was tried to add CURLOPT_SSL_VERIFYPEER but also not working ,
anyone can help me out ?
here is my code :
curl_setopt_array($curl, array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_URL => "https://mysitehttps.domain",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"asdasd\"\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"Authorization: 123HaHaHa",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
"postman-token: 3b3fd06d-a8aa-65db-a917-c911fa0bb5d5"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Thank you
I have tested this code and its working for me.
Your content-type is set to multi-part. Are you sure its not json. anyway try the code below
//initialize data variables that you want to send as post below
$data = array("name" => "Hagrid", "age" => "36");
$data_string = json_encode($data);
$ch = curl_init('https://mysitehttps.domain');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
//'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
"Authorization: 123HaHaHa",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
"postman-token: 3b3fd06d-a8aa-65db-a917-c911fa0bb5d5"
)
);
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
I am using native php 5 (I know it sucks). I want to use curl post with some response
{ SUCCESS = 0,UNAUTHORIZED = 4,WRONG_PARAM = 5,DTIME_OLD = 6,WRONG_SIGN = 11,TOO_LONG = 12}
Actually i've tried on postman and it works with return 0 (SUCCESS).
But when I try on php 5 on localhost XAMPP, it always return empty reply from server.
I also check it on verbose cmd and it still no reply (img:https://i.stack.imgur.com/1zuIx.png).
Any idea, please?
Here is the code:
$path = "msisdn=087884327525&text=meong&sign=".$sign."&userid=19348×tamp=".$date_fix;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('accept: /User-Agent: python-requests/2.8.1',
'accept-encoding: gzip, deflate',
'cache-control: no-cache',
'connection: keep-alive',
'content-length: 119',
'Content-Type: application/x-www-form-urlencoded; charset=utf-8'
// 'postman-token: 53c6e053-1692-010b-ffb9-b249ab94fca1'
));
$response = curl_exec($ch);
if ($response === false){
print_r('Curl error: ' . curl_error($ch));
}else{
echo "success";
}
curl_close($ch);
print_r($response);
ANSWER
I change the 'accept' header and separate it with 'user-agent'. And also, I move the timestamp path to prevent error '×' into 'x'. I ran it on postman (works), then I copy the code from postman to my editor, and it works.
Here is the code:
$curl = curl_init();
curl_setopt_array($curl, array(
// CURLOPT_PORT => "9922",
CURLOPT_URL => $host,
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_POSTFIELDS => "timestamp=".$date_fix."&msisdn=087881257525&text=test%20USSD&sign=".$sign."&userid=19348",
CURLOPT_HTTPHEADER => array(
"accept: /",
"accept-encoding: gzip, deflate",
"cache-control: no-cache",
"connection: keep-alive",
"content-type: application/x-www-form-urlencoded",
"postman-token: 95239f79-13c2-f64e-4fa0-d2498e5118c9",
"user-agent: python-requests/2.8.1"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
I am trying to send some data to an curl URL to obtain a key.
The curl command is:
curl -X POST "http://website.com/api/open/oauth/token" -H "accept: application/json" -H "Authorization: xx:xxxxxxxxxxxxxxxx" -H "content-type: application/x-www-form-urlencoded" -d "grant_type=password&username=xxx#yahoo.com&password=xxxx"
Any idea how this will look into a php code to get the response key ?
You can try like this,
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://website.com/api/open/oauth/token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=password&username=xxx#yahoo.com&password=xxxx");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Accept: application/json";
$headers[] = "Authorization: xx:xxxxxxxxxxxxxxxx";
$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);
Using cURL, you can do something like this:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://website.com/api/open/oauth/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "grant_type=password&username=xxx%40yahoo.com&password=xxxx",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"authorization: xx:xxxxxxxxxxxxxxxx",
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
"postman-token: ac46b74a-6b24-85a4-36ea-4867a3bd3eda"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
You can also use the HttpRequest class:
<?php
$request = new HttpRequest();
$request->setUrl('http://website.com/api/open/oauth/token');
$request->setMethod(HTTP_METH_POST);
$request->setHeaders(array(
'postman-token' => '67cba408-41b5-7c86-568f-437e27abfa08',
'cache-control' => 'no-cache',
'content-type' => 'application/x-www-form-urlencoded',
'authorization' => 'xx:xxxxxxxxxxxxxxxx',
'accept' => 'application/json'
));
$request->setContentType('application/x-www-form-urlencoded');
$request->setPostFields(array(
'grant_type' => 'password',
'username' => 'xxx#yahoo.com',
'password' => 'xxxx'
));
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}