How to access the CURL to get the json data from php - php

How to access the CURL to get the json data from php.
By the way, I am using codeigniter framework.
This is curl data:
curl -u <YOUR_KEY_ID>:<YOUR_SECRET> \
-X POST https://api.razorpay.com/v1/orders \
-H "content-type: application/json" \
-d '{
"amount": 50000,
"currency": "INR",
"receipt": "receipt#1"
}'
and this my possible work to cosume it:
$url = 'https://api.razorpay.com/v1/orders';
$curl = curl_init($url);
$data = json_encode(array(
'receipt'=> $orderId,
'amount'=>$amount,
'currency'=> 'INR'
), JSON_FORCE_OBJECT);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json'
//$keyId.':'.$secretKey
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
Please help me to consume it!!
I am in trouble and also in hurry!!

If the response like :
[{"data1":"value1","data1.1":"value1.1"},{"data2":"value2","data2.1":"value2.1"}...]
Use :
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$json = file_get_contents('thelink', false, stream_context_create($arrContextOptions));
$result = json_decode($json, true);
and call it like :
echo $result[0]["data1"];
echo $result[0]["data1.1"];
echo $result[0]["data2"];
echo $result[0]["data2.1"];
If you still want to use curl (example on weatherAPI) :
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://weatherapi-com.p.rapidapi.com/forecast.json?q=London&days=1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"x-rapidapi-host: weatherapi-com.p.rapidapi.com",
"x-rapidapi-key: aaaaxxxxbbbb"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$weather = json_decode($response);;
}
?>

Try this, I keep this for whenever I need to consume and API,
$url = 'https://api.razorpay.com/v1/orders';
$curl = curl_init();
$obj = new StdClass();
$obj->receipt = $orderId;
$obj->amount = $amount;
$obj->currency = 'INR';
$payload = json_encode($obj);
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
//$keyId.':'.$secretKey
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
Get your response and handle any errors, maybe you are just missing a comma in your headers array

Related

PHP, cURL, SendinblueAPI : how to code the filebody in order to import CSV file?

Im writing a script in PHP and cURL using sendinBlue API.
This script have to send contact from a CSV file to my datebase in the website sendinBlue.
This API got a documentation : https://developers.sendinblue.com/reference#importcontacts-1
I would like for exemple import theses 2 contacts :
dupont.jean#aol.com;Dupont;Jean;0645342891;2;2019-12-01 19:00:21;false;true;false
dupont2.jean#aol.com;Dupontt;maurice;0645342892;3;2019-12-01 19:00:21;false;true;false
So i have this code :
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sendinblue.com/v3/contacts/import",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"fileBody\":\"dupont.jean#aol.com;Dupont;Jean;0645342891;2;2019-12-01 19:00:21;false;true;false;
dupont2.jean#aol.com;Dupontt;maurice;0645342892;3;2019-12-01 19:00:21;false;true;false\",
\"listIds\":[2],\"emailBlacklist\":false,\"smsBlacklist\":false,
\"updateExistingContacts\":true,\"emptyContactsAttributes\":true}",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"api-key: ****",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
But this is not working, i have this error : {"error":{"status":400,"message":"Input must be a valid JSON object","code":"bad_request"}}
Ive tried to import only one contact, with only the email with this code :
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sendinblue.com/v3/contacts/import",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"fileBody\":\"dupont.jean#aol.com",
\"listIds\":[2],\"emailBlacklist\":false,\"smsBlacklist\":false,
\"updateExistingContacts\":true,\"emptyContactsAttributes\":true}",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"api-key: ****",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
And this one is working, the contact is in my data base :
But that not enougth because all is empty and i would like to send multiple contact at the same time ! (almost 10 000 contacts!).
Would you please help me please ?
The param fileBody should be similar to .csv or .txt, see the Example here.
And my codes with Guzzle look like this:
$fileBody = "EMAIL;FIRSTNAME;LASTNAME" . " \n" .
"junie3#example.com;Junie;JL" . " \n" .
"john#example.com;John;JohnL";
$client = new Client([
'headers' => [
'api-key' => $apiKey
]
]);
$client->post($apiUrl . '/contacts/import', [
\GuzzleHttp\RequestOptions::JSON => [
'fileBody' => $fileBody,
'listIds' => [$listId],
'updateExistingContacts' => true,
]
]);
I wrote "rowtext" . " \n" because you'll need to enter row text dynamically.
I wrote " \n" instead of "\n" because it doesn't store last column data (LASTNAME in my example) just with "\n".
It won't work if you change double quotes to single ones.

IP long string filter

Hey,
i am trying to only show 1 bit of the long string i get from the api i got fram postman, the only thing i need to show is the city. How do i need do this?
i'm trying to find a way with php but i have no clue what to do
a:14:{s:10:"regionName";s:10:"California";s:6:"status";s:7:"success";s:4:"city";s:13:"Mountain View";s:8:"timezone";s:19:"America/Los_Angeles";s:7:"country";s:13:"United States";s:11:"countryCode";s:2:"US";s:3:"zip";s:0:"";s:3:"lon";d:-122.08499908447266;s:3:"isp";s:6:"Google";s:2:"as";s:19:"AS15169 Google Inc.";s:5:"query";s:7:"8.8.8.8";s:6:"region";s:2:"CA";s:3:"lat";d:37.42290115356445;s:3:"org";s:6:"Google";}
(im using the ip of google just for this question)
so the length of the city name changes!
the site where i got it frm http://ip-api.com/php/8.8.8.8
and the code i am using:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://ip-api.com/php/8.8.8.8",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"postman-token: 2e83e542-a6fb-5bb6-94e0-c1908282a2a2"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
That's a product of running a PHP variable in a PHP serialize you can reverse it with unserialize
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://ip-api.com/php/8.8.8.8",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"postman-token: 2e83e542-a6fb-5bb6-94e0-c1908282a2a2"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
$responseArray = unserialize($response); //You probably need some error trapping here
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $responseArray["country"];
}

Upload video to jw player account using Laravel

I want to add video file or video URL to jwplayer in my laravel 5.2 project but i didn't found it's any package or library yet. It's documentation is very difficult to understand for me as I never worked on jwplayer before. My requirement is to upload video to jwplayer and amazon s3 bucket. It's uploading to s3 bucket but I'm stuck at jwplayer. Any guide or example will be helpful.
Use this code...
<?php
require_once('vendor/autoload.php');
function get_parts_count($file) {
// 5 MB is the minimum size of an upload part, per https://developer.jwplayer.com/jwplayer/docs/stream-multipart-resumable-upload
$minimum_part_size = 5 * 1024 * 1024;
$size = filesize($file);
return (floor($size / $minimum_part_size) + 1);
}
$secret = $_ENV('JWPLATFORM_API_SECRET');
$site_id = $_ENV('JWPLATFORM_SITE_ID');
$jwplatform_api = new Jwplayer\JwplatformClient($secret);
$target_file = 'examples/test.mp4';
$params = array();
$params['metadata'] = array();
$params['metadata']['title'] = 'PHP API Test Upload';
$params['metadata']['description'] = 'Video description here';
$params['upload'] = array();
$params['upload']['method'] = 'multipart';
$create_response = json_encode($jwplatform_api->Media->create($site_id, $params));
print_r($create_response);
print("\n");
$decoded = json_decode(json_decode(trim($create_response), true), true);
$upload_id = $decoded['upload_id'];
$upload_token = $decoded['upload_token'];
$media_id = $decoded['id'];
$parts_count = get_parts_count($target_file);
$upload_parts_url = "https://api.jwplayer.com/v2/uploads/$upload_id/parts?page=1&page_length=$parts_count";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $upload_parts_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(
'Authorization: Bearer ' . $upload_token,
),
));
$upload_parts_response = curl_exec($curl);
curl_close($curl);
$upload_parts_response_object = json_decode($upload_parts_response);
print_r($upload_parts_response_object);
print("\n");
$upload_url_parts = $upload_parts_response_object->parts;
// 5 MB is the minimum size of an upload part, per https://developer.jwplayer.com/jwplayer/docs/stream-multipart-resumable-upload
$buffer = (1024 * 1024) * 5;
if (count($upload_url_parts) > 0) {
$file_handle = fopen($target_file, 'rb');
foreach($upload_url_parts as $upload_url_parts_key => $upload_url_parts_value) {
$upload_bytes_url = $upload_url_parts_value -> upload_link;
$file_part = fread($file_handle, $buffer);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $upload_bytes_url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_POSTFIELDS, $file_part);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type:'
));
$upload_bytes_response = curl_exec($curl);
print_r($upload_bytes_response);
print("\n");
}
fclose($file_handle);
$complete_upload_url = "https://api.jwplayer.com/v2/uploads/$upload_id/complete";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $complete_upload_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 => 'PUT',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer ' . $upload_token,
),
));
$complete_upload_response = curl_exec($curl);
curl_close($curl);
$complete_upload_response_object = json_decode($complete_upload_response);
print_r($complete_upload_response_object);
print("\n");
}
?>
From what I can tell Laravel is a PHP framework and it shouldn't be a problem to use the JW Player APIs within that. Have you checked the documentation here:
https://developer.jwplayer.com/jw-platform/reference/v1/s3_uploads.html
There are also a couple of script examples (PHP and Python) that you can use here to get started:
https://developer.jwplayer.com/jw-platform/
There are multiple steps in uploading a video on jwplayer:-
$apiKey = env('jw_player_apikey');
$apiV2Secret = env('jw_player_secretkey);
// Step1. for getting upload_token and upload_id
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.jwplayer.com/v2/sites/$apiKey/media/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"upload\":{\"method\":\"multipart\"},\"metadata\":{\"title\":\"$r->title\",\"description\":\"My media description\"}}",
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Authorization: $apiV2Secret",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return response()->json(['status'=>'error']);
} else {
$response=json_decode($response);
// Step2. For getting upload_link
$curl1 = curl_init();
curl_setopt_array($curl1, [
CURLOPT_URL => "https://api.jwplayer.com/v2/uploads/$response->upload_id/parts?page=1&page_length=1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Authorization: Bearer ".$response->upload_token,
"Content-Type: application/json"
],
]);
$response1 = curl_exec($curl1);
$err1 = curl_error($curl1);
curl_close($curl1);
if ($err1) {
return response()->json(['status'=>'error']);
}
else {
$response1=json_decode($response1);
$url = $response1->parts[0]->upload_link;
// Step4. For uploading parts
$curl2 = curl_init($url);
curl_setopt($curl2, CURLOPT_URL, $url);
curl_setopt($curl2, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl2, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($curl2, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl2, CURLOPT_HTTPHEADER, array('Content-Type:'));
$file = $r->file('video');
$handle = fopen($file, "r");
$data = fread($handle, filesize($file));
curl_setopt($curl2, CURLOPT_POSTFIELDS, $data);
$resp = curl_exec($curl2);
$err2 = curl_error($curl2);
curl_close($curl2);
if ($err2) {
return response()->json(['status'=>'error']);
}
else {
// Step 5. to complete the upload
$curl3 = curl_init();
curl_setopt_array($curl3, [
CURLOPT_URL => "https://api.jwplayer.com/v2/uploads/".$response->upload_id."/complete",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer ".$response->upload_token,
"Content-Type: application/json"
],
]);
$response3 = curl_exec($curl3);
$err3 = curl_error($curl3);
curl_close($curl3);
if ($err3) {
return response()->json(['status'=>'error']);
} else {
// Step 6. Get uploaded video details
$curl6 = curl_init();
curl_setopt_array($curl6, [
CURLOPT_URL => "https://api.jwplayer.com/v2/sites/$apiKey/media/$response->id",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer $apiV2Secret",
"Content-Type: application/json"
],
]);
$response6 = curl_exec($curl6);
$err6 = curl_error($curl6);
curl_close($curl6);
if ($err6) {
return response()->json(['status'=>'error']);
} else {
$video_response=json_decode($response6);
//get video id from the response
if($this->check_count($video_response)>0){
//insert video detail in database
$insert=new videos();
$insert->jw_id=$video_response->id;
$insert->save();
if ($insert) {
return response()->json(['status'=>'success']);
} else {
return response()->json(['status'=>'error','message'=>"Fail to upload video, Please try again!!"]);
}
}
else{
return response()->json(['status'=>'error','message'=>"Fail to upload video, Please try again!!"]);
}
}
}
}
}
}
The above code doc was received from JW player official. It's working on my side. Hope it helps you too.

ALM SaaS REST API 12.50 not working in PHP

I have tried below code in PHP to get the defect details from ALM but its not showing any response in browser. But the same is working in POSTMAN . Can someone help me here
Here is the document of REST API USAGE REST DOCUMENT FROM ALM
I have already tried existing posts from Stackoverflow
HP ALM REST API login using PHP CURL
ALM REST API v12.50 error 401
Nothing is helping so posted a new question
Note : Due to security purpose header value is kept as encoded value
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://hostname/qcbin/api/domains/domainname/projects/projectname/defects/?limit=10",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Basic encoded value",
"cache-control: no-cache",
"postman-token: a8a2398d-7a0a-0ebd-a586-58a40e524a9a"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
I have finally found the solution and below is the approach
First we need get to LWSSO_COOKIE_KEY,QCSession,ALM_USER,XSRF_TOKEN values from the ALM Authentication link then we should use the values for subsequent calls
Below is the complete working code to get the list of defects by entering ALM Credentials
<?php
$curl = curl_init();
Header('Content-type: application/json');
$credentials = "username:password";
curl_setopt_array($curl, array(
CURLOPT_URL => "https://host:port/qcbin/api/authentication/sign-in",
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HEADER => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Basic " . base64_encode($credentials) ,
"cache-control: no-cache"
) ,
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err)
{
echo "cURL Error #:" . $err;
}
else
{
// If there is no error then get the response to form the array of headers to get the different values required
$array_start = explode(';', $response);
foreach ($array_start as $key => $value) {
$remove_from_string = ['HTTP/1.1 200 OK','Path=/','HTTPOnly','HttpOnly','Content-Length',': 0'];
$replace_array = ['','','','','',''];
$value = str_replace($remove_from_string,$replace_array,$value);
$value = trim(preg_replace(('/Expires: [a-zA-Z]+, [0-9]+ [a-zA-Z]+ [0-9]+ [0-9]+:[0-9]+:[0-9]+ [a-zA-Z]+/'), '', $value));
$value = trim(preg_replace(('/Server: [a-zA-Z0-9.\(\)]+/'),'',$value));
if (!empty($value)) {
$almheaders[trim(explode('=',$value)[0])] = explode('=',$value)[1];
}
}
$LWSSO_COOKIE_KEY = $almheaders['Set-Cookie: LWSSO_COOKIE_KEY'];
$QCSession = $almheaders['Set-Cookie: QCSession'];
$ALM_USER = $almheaders['Set-Cookie: ALM_USER'];
$XSRF_TOKEN = $almheaders['Set-Cookie: XSRF-TOKEN'];
// Now form the Cookie value from the above values.
$cookie = "Cookie: JSESSIONID=33eyr1y736486zcnl0vtmo12;XSRF-TOKEN=$XSRF_TOKEN;QCSession=$QCSession;ALM_USER=$ALM_USER;LWSSO_COOKIE_KEY=$LWSSO_COOKIE_KEY";
// echo $cookie;
$curl = curl_init();
Header('Content-type: application/json');
curl_setopt_array($curl, array(
CURLOPT_URL => "https://host:port/qcbin/api/domains/CET_NTD/projects/BILLING_OPERATIONS/defects",
// CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Basic " . base64_encode($credentials) ,
"cache-control: no-cache",
"Accept: application/json",
$cookie
) ,
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err)
{
echo "cURL Error #:" . $err;
}
else
{
echo $response;
}
}
?>

Passing variable in curlopt_url

I want to pass a variable in CURLOPT_URL, here is my code
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.postmates.com/v1/customers/cus_KtQih0aARUZXdk/deliveries/$delivery_id/cancel",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array(
'pickup_address' => $_POST["pickup_address"],
'pickup_phone_number' => $_POST["pickup_phone_number"],
'pickup_name' => $_POST["pickup_name"],
'dropoff_address' => $_POST["dropoff_address"],
'dropoff_phone_number' => $_POST["dropoff_phone_number"],
'dropoff_name' => $_POST["dropoff_name"],
'manifest' => $_POST["manifest"]
),
CURLOPT_HTTPHEADER => array(
"authorization: Basic MjhiMDU0ODktNjdkYS00M2VhLTg0NmMtYWQ1MWQ2MGNmMDA1Og==",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=---011000010111000001101001",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
$delivery_id = trim($_POST['show_name']);
curl_close($curl);
$response = json_decode($response);
$timestamp = json_decode($dateJSON, true);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response->fee;
}
I want to add the variable $delivery_id in the url, but I am not too familiar with cURL and the above written code is not working. Please show me the way to include this variable in my url.
Once I had the same error. The solution is : use ' -apostrophe - and . -dot- before and after the variable. Like this:
/deliveries/'.$delivery_id.'/cancel

Categories