Not Able to Get Value Variable into Curl Php - php

I am trying to pass the Variable into Curl But It's Not Working as I am trying to Call JSON API I Tried all The Possible Ways But Ntg Worked.
$name = "SAI";
$amount = "42000";
$user = "1643031393";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.interakt.ai/v1/public/track/events/",
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 =>"{\"userId\":\"1643031393\",\"event\":\"SEVA BILL GENRATED\",\"traits\": {\"donated\":\"YES\",\"seva_name\":\"$name\",\"seva_amount\":\"$amount\",\"Seva Date\":\"12-22-2022\",\"E-Receipt URL\":\"https://ack.saaa.org"},\"createdAt\": \"2022-01-24T13:26:52.926Z\"}",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Basic gfdsds"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;````

The value of CURLOPT_POSTFIELD option must be JSON format!
Like this:
$data = json_encode(array(
"name" => "Steve",
"amount" => 12345,
"user" => 123456789
));
$curl = curl_init();
curl_setopt_array($curl, array(
...
CURLOPT_POSTFIELDS => $data,
...
));

Related

Apply value into cURL on PHP

I have a question about curl.
Here is the value which will be changed by the user input.
$uid = "xxxxx" //Get from system
$token = "xxxxx" //Get from system
$title = "Testing Message"; //user input
$body = "Message"; //user input
$url_on_click = "https://www.xxxxx.com"; //user input
I want to apply the value into the function but it getting error:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://bn.xxxxxx.com/wpn/wpn_send',
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 =>'{
"uid" :' . $record_uid . ',
"payload": ' . $payload . '
}',
CURLOPT_HTTPHEADER => array(
'token: ' . $record_token . ',
Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
If I change all the value to string. It's work. The follow code like :
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://bn.xxxxxx.com/wpn/wpn_send',
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 =>'{"uid" :"xxxxxx",
"payload": {
"title": "Testing Message",
"body": "1234",
"icon":"https://xxxxx.net/xxxxx.png",
"urlOnClick":"xxxxx.com"
}}',
CURLOPT_HTTPHEADER => array(
'token: xxxxxx',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
May I know which part I did wrong? That issue really pull my hair out.
Thank you =]
Try this, post fields depend on your CURLOPT_HTTPHEADER, according to your header you need to convert it in JSON otherwise, the array type will work
$postFields = array(
'uid' => "xxxx",
'token' => "xxxx",
'title' => "xxxx",
'body' => "xxxx",
'url_on_click' => "xxxx"
);
//curl init lines
//-------
//-------
CURLOPT_POSTFIELDS => json_encode($postFields),

Getting a response through Curl URL in PHP

I am trying to perform some function based on the response I will get from Curl URL. Below is the code I have written. But the response coming is always empty. Due to which the control always goes into an else condition.
if(isset($_POST['verify_button'])){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://*******/v1/****/*********/".$_POST['********']."?MVNO=*******",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic Z*******",
"accept: application/json"
),
));
$response = curl_exec($curl);
curl_close($curl);
$data = json_decode($response);
if($data->resultCode == '0'){
echo '<div class="mm_success_alert">Sucess</div>';
}
else{
echo '<div class="mm_danger_alert">Fail.</div>';
}

PHP cURL request headers isn't working with a global variable

I've set a global variable to equal an access token, and when I print_($session_id) the token is visible. What I'd like to do is pass that variable into my CURLOPT_HTTPHEADER, and have it equal to "Session" in the function get_field_data_id() below:
function get_session_id() {
global $session_id;
$curl = curl_init();
$session_url = "/auth";
curl_setopt_array($curl, array(
CURLOPT_URL => $session_url ,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Accept: application/json",
"username: USER",
"password: PASS",
"authorizationkey: KEY"
),
));
$response = curl_exec($curl);
curl_close($curl);
$session_id = $response;
} add_filter( 'gform_after_submission_4', 'get_session_id' );
function get_field_data_id() {
global $session_id;
$curl = curl_init();
$file_url = "URL";
curl_setopt_array($curl, array(
CURLOPT_URL => $file_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Accept: application/json",
"Session: {$session_id}"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;}
However when I run this function, I get an error message that the session ID is invalid. What am I missing?
{"Message":"Invalid Authorization. Session ID is invalid. "}

How to properly get onedrive access token via php and curl

Am trying to get onedrive access token.
Here is the Request:
GET https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={client_id}&scope={scope}
&response_type=token&redirect_uri={redirect_uri}
Here is Response:
https://myapp.com/auth-redirect#access_token=EwC...EB
&authentication_token=eyJ...3EM&token_type=bearer&expires_in=3600
&scope=onedrive.readwrite&user_id=3626...1d
I have implemented the two code below in other to get the access token but am having error below when page is being redirected for login.
AADSTS900144: The request body must contain the following parameter: 'client_id'.
Scope property is space seperated hence see onedrive documentation link
code 1
$data_string = array(
"client_id" => "myclient id",
"response_type" => "token",
"redirect_uri" => "http://localhost/test/callback.php"
"scope" => ['files.readwrite', 'offline_access'],
);
$data = json_encode($data_string);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "$data",
));
$result = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
code 2
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=myclientid-goes-here&scope=files.readwrite.all offline_access
&response_type=token&redirect_uri=http://localhost/test/callback.php",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
//CURLOPT_POSTFIELDS => "$data",
));
$result = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
This was resolved using http_build_query() function
hence
$data = http_build_query($data_string, '', '&');

Update a Page with PHP Variables / REST API Confluence

I want to update a page in confluence with some php-variables. So here's my PHP Code to update the page:
$curl = curl_init();
$post = "{\"id\":\"65604\",\"type\":\"page\",\"title\":\"page\",\"space\":{\"key\":\"***\"},\"body\":{\"storage\":{\"value\":\"<p>Here comes the other variable: $product_response </p>\",\"representation\":\"storage\"}},\"version\":{\"number\":11}}";
curl_setopt_array($curl, array(
CURLOPT_PORT => "6003",
CURLOPT_URL => "http://localhost:6003/rest/api/content/65604",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => $post,
CURLOPT_COOKIE => "JSESSIONID=3A16CBFE8B99E619D62BD4CD6573F184",
CURLOPT_HTTPHEADER => array(
"authorization: Basic xyYS123_test_test-45Sdasds==",
"content-type: application/json"
),
));
For your information:
If i print the $post in another script, the variable value show up
without the variable, the curl session works and I can update the page
Thats the error:
{"statusCode":500,"message":"org.codehaus.jackson.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be included in string value\n at [Source: com.atlassian.confluence.plugins.restapi.filters.LimitingRequestFilter$1#5dff62ce; line: 1, column: 119]","reason":"Internal Server Error"}
The json values for post that you are forming is incorrectly formed, Why dont you use json_encode(); by declaring all the values inside an array, you can change your code like this:
$curl = curl_init();
$post = array(
"id"=>"65604",
"type"=>"page",
"title"=>"page",
"space"=>["key"=>"***"],
"body" =>["storage"=>["value"=>"<p>Here comes the other variable: ".$product_response." </p>", "representation"=>"storage"]],
"version"=>["number"=>11]
);
curl_setopt_array($curl, array(
CURLOPT_PORT => "6003",
CURLOPT_URL => "http://localhost:6003/rest/api/content/65604",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => json_encode($post),
CURLOPT_COOKIE => "JSESSIONID=3A16CBFE8B99E619D62BD4CD6573F184",
CURLOPT_HTTPHEADER => array(
"authorization: Basic xyYS123_test_test-45Sdasds==",
"content-type: application/json",
'Accept: application/json'
),
));

Categories