Curl didn't work PHP, always return empty esponse - codeigniter

I always get the return empty when I call my api with this code, but it work on my Insomnia, and on my shell
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->body));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$return_data = curl_exec($ch);
curl_close($ch);
if ($return_data) return $return_data;
else return "empty";
my curl_getinfo return :
[content_type] => text/html;charset=UTF-8
[http_code] => 500
[header_size] => 543
[request_size] => 164
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.269685
[namelookup_time] => 0.002741
[connect_time] => 0.024587
[pretransfer_time] => 0.157608
[size_upload] => 636
[size_download] => 0
[speed_download] => 0
[speed_upload] => 2364
[download_content_length] => -1
[upload_content_length] => 636
[starttransfer_time] => 0.157693
[redirect_time] => 0

I fix my problem, thanks to insomnia who can generate PHP code for cURL,
I put the answer in case someone need it
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $this->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 => json_encode($body),
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Accept-Charset: UTF-8",
"Content-Type: application/json"
],
]);
$response = json_decode(curl_exec($curl));

Related

404 not found after changing header content-type from application/json to multipart/form-data laravel

public function customHeaders()
{
return [
'Authorization' => request()->header('Authorization'),
'Content-Type' => 'multipart/form-data',
'Access-Control-Allow-Credentials' => true,
];
}
public function post(Request $request)
{
$response = Http::withHeaders($this->customHeaders())->post($this->url, $request->all());
return response()->json($response->json(), $response->status());
}
I have no problem using application/json but I get 404 when using multipart/form-data
public function customHeaders()
{
return (
'multipart' => [
'Authorization' => request()->header('Authorization'),
'Content-Type' => 'multipart/form-data',
'Access-Control-Allow-Credentials' => true,
'Content-Disposition' => 'multipart',
'name' => 'multipart'
],
'form-data' => [
'Authorization' => request()->header('Authorization'),
'Content-Type' => 'application/json',
'Access-Control-Allow-Credentials' => true,
'Content-Disposition' => 'form-data',
'name' => 'form-data'
]
);
}

Tried to made an POST request using Guzzle

Im so new using API in my project, so i tried to make an POST request using Guzzle in Laravel, but i really dont know to do it, i've been seraching on the internet how to but i can't find the answer, here's what i've been tried:
$headers = [
'Content-Type' => 'application/json',
'signature' => '73ceef837b9be3cf098eca4a4697bd6a36718b64b0cf407c4324415941ff9780',
'va' => '0000002298436631',
'timestamp', '20191209155701'
];
$body = '{
"name": "Dudy",
"phone": "082298436631",
"email": "muhammadmaududy4#gmail.com",
"amount": "10000",
"notifyUrl": "https://mywebsite.com",
"expired": "24",
"expiredType": "hours",
"comments": "Catatan",
"referenceId": "1",
"paymentMethod": "qris",
"paymentChannel": "qris"
}';
$request = new Request('POST', 'https://sandbox.ipaymu.com/api/v2/payment/direct',
$headers, $body);
And i tried to do it on postman and its work perfectly, here's the setting on my postman:
You can use Laravel's Http Facade to deal with requests (easier to test later, if needed):
$headers = [];
$body = [];
$request = Http::send(
method: 'POST',
url: 'https://your.url/',
options: [
'headers' => $headers,
'form_params' => $body,
]
);
$response = Http::withHeaders([
'X-First' => 'foo',
'X-Second' => 'bar'
])->post('http://example.com/users', [
'name' => 'Taylor',
]);
I show a example code on top. I provide your code below.
$response = Http::withHeaders ([
'Content-Type' => 'application/json',
'signature' => '73ceef837b9be3cf098eca4a4697bd6a36718b64b0cf407c4324415941ff9780',
'va' => '0000002298436631',
'timestamp', '20191209155701'
])->post('https://sandbox.ipaymu.com/api/v2/payment/direct',[
"name": "Dudy",
"phone": "082298436631",
"email": "muhammadmaududy4#gmail.com",
"amount": "10000",
"notifyUrl": "https://mywebsite.com",
"expired": "24",
"expiredType": "hours",
"comments": "Catatan",
"referenceId": "1",
"paymentMethod": "qris",
"paymentChannel": "qris"
]);
Try this
$client = new \GuzzleHttp\Client();
$response = $client->post(
'https://sandbox.ipaymu.com/api/v2/payment/direct',
[
'form_params' => [
"name" => "Dudy",
"phone" => "082298436631",
"email" => "muhammadmaududy4#gmail.com",
"amount" => "10000",
"notifyUrl" => "https://mywebsite.com",
"expired" => "24",
"expiredType" => "hours",
"comments" => "Catatan",
"referenceId" => "1",
"paymentMethod" => "qris",
"paymentChannel" => "qris"
]
],
[
'headers' => [
'Content-Type' => 'application/json',
'signature' => '73ceef837b9be3cf098eca4a4697bd6a36718b64b0cf407c4324415941ff9780',
'va' => '0000002298436631',
'timestamp', '20191209155701'
]
]
);
dd($response) //response data

How to set proxy in Http request in Laravel 7?

Below is my successful HTTP request on DEV environment:
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json'
])
->withToken('xxxxxxxxxxxxxx')
->post('https://xxxxxxxxx.com/v0.1/messages/', [
'from' => [
'type' => 'xxxx',
'number' => 'xxxxxxxx',
],
'to' => [
'type' => 'xxxxx',
'number' => 'xxxxxx',
],
'message' => [
'content' => [
'type' => 'text',
'text' => 'test message from laravel'
]
]
]);
But on production its mandatory to add a proxy to the request.
Anyone have any idea how to pass a proxy with the request above ?
Thank you in advance.
You can specify Guzzle options using the withOptions method.
Hence:
$response = Http::withOptions([
'proxy' => 'http://username:password#proxyhost.com:7000'
])->withHeaders( ...

How to pass the Zoho-oauthtoken in the Header using Guzzle on Laravel?

I have this cURL that I want to convert for Guzzle
curl_setopt_array($curl, array(
CURLOPT_URL => "https://subscriptions.zoho.com/api/v1/hostedpages/newsubscription",
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 =>"{\n \"plan\": {\n \"plan_code\": \"AM-001\",\n \"price\": " . $gPrice . ",\n \"tax_id\": \"1786305000000842230\",\n },\n \"addons\": [\n {\n \"addon_code\": \"AB-001\",\n \"addon_description\": \"Ads Budget\",\n \"price\": " . $bPrice . ",\n\n }\n ],\n \"coupon_code\": \"150-credit\"\n \n}",
CURLOPT_HTTPHEADER => array(
"X-com-zoho-subscriptions-organizationid: " . $org_id,
"Authorization: Zoho-oauthtoken " . $accessToken,
"Content-Type: application/x-www-form-urlencoded"
),
));
For now, I have convert this :
$headers = [
'Authorization' => 'Zoho-oauthtoken ' . $access_token,
'X-com-zoho-subscriptions-organizationid' => $org_id,
];
$res = $client->request('POST', 'https://subscriptions.zoho.com/api/v1/hostedpages/newsubscription', $headers, [
'plan' => [
'plan_code' => 'AM-001',
'price' => $data['finaltotal'],
'tax_id' => '1786305000000842230',
],
'addons' =>[
'addon_code' => 'AB-001',
'addon_description' => 'Ads Budget',
'price' => $data['finalads']
],
'coupon_code' => '150-credit'
]);
But I have
"Client error: POST https://subscriptions.zoho.com/api/v1/hostedpages/newsubscription resulted in a 401 Unauthorized response:
{"code":14,"message":"Invalid value passed for authtoken."}"
Have I correctly defined the header?
Thank you for your help.
third option is options for request, So if you need to pass headers you need to specify key headers.
so your code should look like this
$options = [
'headers' => [ // <- here :)
'Authorization' => 'Zoho-oauthtoken ' . $access_token,
'X-com-zoho-subscriptions-organizationid' => $org_id,
];
]
$res = $client->request(
'POST',
'https://subscriptions.zoho.com/api/v1/hostedpages/newsubscription',
$options, // <- options
[
'plan' => [
'plan_code' => 'AM-001',
'price' => $data['finaltotal'],
'tax_id' => '1786305000000842230',
],
'addons' =>[
'addon_code' => 'AB-001',
'addon_description' => 'Ads Budget',
'price' => $data['finalads']
],
'coupon_code' => '150-credit'
]
);
try this it should work.
if any doubt please comment.

Elasticsearch query gives different results from cURL and Kibana

I am a newbie in Elasticsearch and kibana and am trying to connect elasticsearch with PHP to generate some reports.
I am trying to run a query, which runs perfectly fine in kibana sense but surprisingly gives different results whenever I cURL it.
GET /_search
{
"query": {
"filtered": {
"query": {
"query_string": {
"query": "*",
"analyze_wildcard": true
}
},
"filter": {
"bool": {
"must": [
{
"query": {
"query_string": {
"analyze_wildcard": true,
"query": "*"
}
}
},
{
"range": {
"start_time": {
"gte": 1532889000000,
"lte": 1532975399999,
"format": "epoch_millis"
}
}
}
],
"must_not": []
}
}
}
},
"size": 0,
"aggs": {
"2": {
"terms": {
"field": "layers",
"size": 50,
"order": {
"_count": "desc"
}
}
}
}
}
When I cURL this query, it returns documents instead of aggregate results.
Where am I going wrong?
TIA
EDIT: I am working on Elasticsearch 2.3
The PHP Code I am running to retrieve the results:
$jarray=array (
'query' =>
array (
'filtered' =>
array (
'query' =>
array (
'query_string' =>
array (
'query' => '*',
'analyze_wildcard' => true,
),
),
'filter' =>
array (
'bool' =>
array (
'must' =>
array (
0 =>
array (
'query' =>
array (
'query_string' =>
array (
'analyze_wildcard' => true,
'query' => '*',
),
),
),
1 =>
array (
'range' =>
array (
'start_time' =>
array (
'gte' => 1532889000000,
'lte' => 1532975399999,
'format' => 'epoch_millis',
),
),
),
),
'must_not' =>
array (
),
),
),
),
),
'size' => 0,
'aggs' =>
array (
2 =>
array (
'terms' =>
array (
'field' => 'layer_count',
'size' => 50,
'order' =>
array (
'_count' => 'desc',
),
),
),
),
);
$jdata=json_encode($jarray);
$url = '10.10.113.97:9200/my_index/_search';
echo $url.'<br><br><br><br><br><br>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_GETFIELDS, json_encode($jdata));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch) or die(curl_error());
curl_close($ch);
print_r(stripcslashes($return));
In your cURL code, you need to change the field you use in your aggregation section:
'aggs' =>
array (
2 =>
array (
'terms' =>
array (
'field' => 'layers', <--- change this line
'size' => 50,
'order' =>
array (
'_count' => 'desc',
),
),
),
),
Also you need to send your request like this. My worry is that you're sending a GET without any request body and ES will simply ignore your query and perform a generic search.
$query = json_encode($jdata);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($query))
);

Resources