Passing variable in curlopt_url - php

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

Related

Cannot recieve header information via PHP but with Postman [duplicate]

This question already has answers here:
Can PHP cURL retrieve response headers AND body in a single request?
(16 answers)
Closed 3 years ago.
I'm trying to get a Token in the header of an POST request. With Postman it works but when I run this in PHP I get the right content but cannot extract the header information.
I have copied the PHP code generated in Postman but I cannot extract the header information. I only get the content.
The code I used:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://xxxx.yyyy.xy/xxxxxx/xxxxxxxxxxx",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"Content-Type: application/x-www-form-urlencoded",
"Postman-Token: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
The header should include "Token".
I cannot recieve any header information.
I found a solution on the problem:
<?php
$curl = curl_init();
$Username = "XXXXXXXXX";
$Password = "YYYYYYYYYY";
$Credentials = base64_encode($Username.':'.$Password);
$Credentials_Auth = "Authorization: Basic ".$Credentials;
// echo "<br>Cred-auth: ".$Credentials_Auth."<br>";
curl_setopt_array($curl, array(
CURLOPT_URL => "https://XXXXXXXXX/YYYYYYYYYYY/ZZZZZZZZZZZ",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
**CURLOPT_HEADER => 1,**
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
$Credentials_Auth,
"Content-Type: application/x-www-form-urlencoded",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) { echo "cURL Error #:" . $err;}
else { echo $response; }
**$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $header_size);
$body = substr($response, $header_size);**
curl_close($curl);
**header("Content-Type:text/plain; charset=UTF-8");
echo $headers;
echo $body;**
?>

foreach only running function (`sendThis`) for first item in array

I'm trying to send each item in an array to a function. The function is a curl request, but I don't think that matters.
$a=array('string1','string2');
foreach ($a as $value) {
sendThis($value);
}
That code runs the function just once for the first item in the array (string1). How do I keep this running for every item in the array?
sendThis function is similar to the following:
function sendThis($value){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.xyz.com/v2/users/".$value."/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"Authorization: XYZ123",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}}

Acumatica REST Contract-Based API and PHP Curl As ERROR Has Occured to Create Data

I've been trying to create customer in Acumatica contract-based REST API following TIM RODMAN method and a little tweak of code
and all I'm getting is an error
{"message":"An error has occurred."}
I have tried to get data (GET all data) has been successful, but when I try to create new data customer, purchase order or else i got an error appears as above
Note: The same create in Postman didn't work, but start from login, get data, and logout work fine.
See the code below for my latest version of simplified code
function login_acumatica($cookie_jar, $curl){
// Login to Acumatica REST API
curl_setopt_array($curl, array(
CURLOPT_URL => "http://111.11.111.11/AcumaticaMMI/entity/auth/login",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_COOKIESESSION => 1,
CURLOPT_COOKIEJAR => $cookie_jar,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\r\n \"name\": \"admin\",\r\n \"password\": \"1112345\",\r\n \"company\": \"DUMMY USER\"\r\n}",
CURLOPT_HTTPHEADER => array( "cache-control: no-cache", "content-type: application/json"),
));
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
}
function logout_acumatica($cookie_jar, $curl){
// Logout of Acumatica REST API
curl_setopt_array($curl, array(
CURLOPT_URL => "http://111.11.111.11/AcumaticaMMI/entity/auth/logout",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_COOKIESESSION => 1,
CURLOPT_COOKIEFILE => $cookie_jar,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array( "cache-control: no-cache", "content-type: application/json"),
));
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
// Close Connection
curl_close($curl);
// Remove Cookie Jar
unlink($cookie_jar) or die("Can't unlink $cookie_jar");
}
switch ($_GET['query']) {
case 'create_customer':
// Add Cookie Jar
$cookie_jar = tempnam('/tmp','cookie.txt');
// Initiate Connection
$curl = curl_init();
login_acumatica($cookie_jar, $curl);
curl_setopt_array($curl, array(
CURLOPT_URL => "http://111.11.111.11/AcumaticaMMI/entity/Default/6.00.001/CUstomer",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => "{\n\t\"CustomerID\": {\"value\":\"C-00023\"},\n\t\"CustomerName\": {\"value\":\"Cust Test 1\"}\n}",
CURLOPT_HTTPHEADER => array(
"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;
}
logout_acumatica($cookie_jar, $curl);
break;
default:
echo dirname(__FILE__) . '/cookie.txt';
break;
}
sorry for my bad english. thanks in advance
Unless you're using OAuth (which you are not), Acumatica requires cookies for the authentication to work. Postman handles cookies autimatically. As far as I can see, you don't transfer cookies between login call and subsequent calls, which is why your setup doesn't work.
Try something like this from Tim Rodman
// Add Cookie Jar
$cookie_jar = tempnam('/tmp','cookie');
// Initiate Connection
$curl = curl_init();
// Login to Acumatica REST API
echo "START <br><br>";
curl_setopt_array($curl, array(
CURLOPT_URL => "http://111.11.111.11/AcumaticaIII/entity/auth/login",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_COOKIESESSION => 1,
CURLOPT_COOKIEJAR => $cookie_jar,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\r\n \"name\": \"admin\",\r\n \"password\": \"123\",\r\n }",
CURLOPT_HTTPHEADER => array( "cache-control: no-cache", "content-type: application/json", "postman-token: e0a0ff40-8d46-4c5f-106b-960ad1aafba8"
),
));

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"];
}

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

Categories