No response from curl operation but success in postman - php

The below one i tested with postman its returning the response but when i check in the PHP its not returning any response.please help me.
cURL file creation:
$curl_file = curl_file_create(\Yii::$app->basePath.'/web/uploads/'.$uploadedFile->name,'pbix',$uploadedFile->baseName);
$params = ['file' => $curl_file];
cURL Process:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://***.***.**.***/v1.0/collections/**/workspaces/**/***?datasetDisplayName=new test",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $params,
CURLOPT_HTTPHEADER => array(
"authorization: key",
),
));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

The problem is space in the parameter.The space should be replaced by %20. you need to do like below:
$url="https://***.***.**.***/v1.0/collections/**/workspaces/3b09eff6-69ae-4932-9419-14eeb69a9dbd/***?datasetDisplayName=new test";
$curl_url=str_replace(" ","%20",$url);
or you have to use urlencode.This is used when encoding a string to be used in a query part of a URL.
$curl_url="https://***.***.**.***/v1.0/collections/**/workspaces/3b09eff6-69ae-4932-9419-14eeb69a9dbd/***?datasetDisplayName=urlencode(new test)";
Then
curl_setopt_array($curl, array(
CURLOPT_URL => $curl_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $params,
CURLOPT_HTTPHEADER => array(
"authorization: key",
),
));

Related

Image upload through API using php curl not working

I am trying to upload the image to database using api
$curl = curl_init();
$headers = array("Content-Type: multipart/form-data",);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt_array($curl, array(
CURLOPT_URL => 'api 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 => 'POST',
CURLOPT_POSTFIELDS => array('file'=> new CURLFILE($_FILES['cfile']['tmp_name'])),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
In this code image is not getting store as expected CURLOPT_POSTFIELDS => array('file'=> new CURLFILE($_FILES['cfile']['tmp_name']))

Getting Data from API - PHP

I have been trying to connect to an API, which requires both a user and password key. I was able to connect using curl from the command line, however am having trouble using PHP.
I have tried using "user:password" as the AUTH key (which worked in curl), previously the code simply required a password and worked.
Below is the code that I am using:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "api-link-removed.com" . urlencode($location),
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(
"accept: application/json",
"authorization: Basic " . AUTH_KEY,
"content-type: application/json"
),
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
));
$response = curl_exec($curl);
$err = curl_error($curl);
Any suggestions to what I might be doing wrong would be much appreciated!
Thanks in advance!
Maybe Guzzle HTTP
$client = new GuzzleHttp\Client();
$response = $client->get('http://www.server.com/endpoint', [
'auth' => [
'username',
'password'
]
]);
https://docs.guzzlephp.org/en/stable/
I tried this:
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt_array($curl, [
CURLOPT_URL => "your api url here",
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 => [
"Name of api host: the name here",
"Api key : api key here"
]
);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

Curl request is refusing connection to server using POST method

I am sending a post request to a url with following parameters:
{"invoice":"INV-521240","status":1,"method":"wallet","re":"testing_ref"}
I am testing same request with postman and it works. The problem is that the request isn't working from curl request I am sending:
Here is the code I am using:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "7080",
CURLOPT_URL => "http://47.91.109.79:7080/moolapay/status",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"invoice\":\"INV-521240\",\"status\":1,\"method\":\"wallet\",\"re\":\"testing_ref\"}",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json",
"postman-token: b7732a5b-2d0d-1f9f-7557-b14a696385e9"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

How to generate dynamic authorization code using JWT api, user and password

Right now i am able to get the data from api by using below code:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://test-api.test.org/user/token",
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 eyJ0eXAiOiJKV1QiI1NiJ9.eyJhdWQiOiIxIiwianRpIjoN2ZiQwiZXhwIjoxNTk0MjY5NjI"
),
));
$response = curl_exec($curl);
$data = json_decode($response, true);
echo $response;
But authorization code is expiring every 15 min and i have to refresh the token after that interval in php.
My requirement is:
To generate token dynamically by using jwt api, user and password
check for if the token is valid or expired after sometime
once the token is expired then refresh it and generate a new token dynamically.
Thanks in advance for all your support.
I found solution of my problem and its working perfectly fine.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://{instance}/app/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 => "{\"username\":\"yourusername\", \"yourpassword\":\"{pass}\"}",
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;
}

Passing form variable into php curl request

I've searched the site extensively and I just can't get the syntax correct in my attempt to replace a parameter in a Curl request with data inserted from a form.
This code works:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "8000",
CURLOPT_URL => "http://nd-337-495-552.rg-709-313.p2pify.com:8000",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "{\"method\":\"liststreamitems\",\"params\":[\"Device\"],\"chain_name\":\"nw-504-042-6\"}",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic abcdefghijklmnop1234567=",
"Content-Type: application/json",
"Postman-Token: dd629e44-26fb-4ed1-ac4e-c47faf38c356",
"cache-control: no-cache"
), // auth header content deliberately changed
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
This code, where I try to replace "nw-504-042-6\" with the variable $ChainName in CURLOPT_POSTFIELDS does not work.
<?php
$ChainName = $_POST['ChainName'];
$StreamName = $_POST['StreamName'];
echo $ChainName;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "8000",
CURLOPT_URL => "http://nd-337-495-552.rg-709-313.p2pify.com:8000",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "{\"method\":\"liststreamitems\",
\"params\":[\"Device\"],
\"chain_name\":"'"$ChainName"'"}",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic abcdefghijklmnop1234567=",
"Postman-Token: 0bcbbec4-cc25-4059-b2c4-220c4722b9d5",
"cache-control: no-cache"
), // auth header content deliberately changed
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Would appreciate the assistance
Try to json_encode your params
$params = [
"method"=>"liststreamitems",
"params"=>["Device"],
"chain_name"=>$ChainName
];
Then pass it to the option CURLOPT_POSTFIELDS like this
CURLOPT_POSTFIELDS => json_encode($params);

Categories