Export HTTP POST request with file from Postman not working - php

I have a Postman HTTP request using POST, a form data field of a file saved under the key plsm_xls_file[].
The file is in the local filesystem.
This request runs perfectly from POSTMAN but when I try to export it to PHP-Curl from the Code Snippets I get something like this:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://mydomain.nl/po_upload3.php?xlsimport=2',
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('plsm_xls_file[]'=> new CURLFILE('/C:/Users/myuser/Documents/vita_debug/201216_FG_PC_68715.xlsx'),'template_id' => '170'),
CURLOPT_HTTPHEADER => array(
'cookie: PHPSESSID=509e15pepo3ok80nd74jhdis33;'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
It's not working. It's like the file isn't properly attached to the HTTP request.
EDIT: I finally understood that the problem was that POSTMAN has access to my filesystem but the remote server where I tried to run the exported snippet don't- A very silly mistake on my side.

I had this problem a while back, and while there was never a true resolution (even in PHP bug tracker), I was able to fix it by not including the file in the setopt_array command.
PHP 7.2 CURLFile Gives "Invalid Filename" Warning
In short, try taking your CURLOPT_POSTFIELDS option out of the curl_setopt_array call and add this:
curl_setopt($curl, CURLOPT_POSTFIELDS, array('plsm_xls_file[]'=> new CURLFILE('/C:/Users/myuser/Documents/vita_debug/201216_FG_PC_68715.xlsx'),'template_id' => '170'));

Related

Problem to get Data with CURL from an API "*.glitch.me" Domain

I am trying to get the JSON data from a Website called
"https://thesimpsonsquoteapi.glitch.me/"
It has some sort of protection and take a while to open.
The Problem. After the site is loaded, you get access to some API calls like
"https://thesimpsonsquoteapi.glitch.me/quotes"
If I try it in Postman, I get back the right JSON response also if I open it up in the browser.
But if I try the to load the Data via curl in PHP i get the html code back from the first protection side. What can I do to get the JSON like in Postman.
$curl = curl_init();
$url = 'https://thesimpsonsquoteapi.glitch.me/quotes?count=1';
curl_setopt_array($curl, array(
CURLOPT_URL => $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',
));
$response = curl_exec($curl);
curl_close($curl);
thx for any help

Unable to POST data TO REST API Using PHP CURL but it work fine with Postman

I am trying to post a data to REST API using PHP/CURL and seems it's not working, as i get 301 Moved Permanently error,But it works fine with Postman. Here is the snippet of PHP CURL which i generated form Postman
$curl = curl_init();
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('text' => 'test'),
CURLOPT_HTTPHEADER => array(
'api_key: key',
'Content-Type:application/json',
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
And it always return 301 Moved permanently, Note if i change the API key i got Unauthorised error, which means it hits the server, but i am not sure what i am missing, I have tried with multiple headers combinations.
Any help on this regard would be highly appreciated.
Quoting the PHP manual :
CURLOPT_POSTFIELDS: This parameter can either be passed as a urlencoded
string like 'para1=val1&para2=val2&...' or as an array with the field
name as key and field data as value. If value is an array, the
Content-Type header will be set to multipart/form-data.
Since you use array you'll need to use multipart.
See this post : curl POST format for CURLOPT_POSTFIELDS

Requesting a cURL GET Request in PHP outputs garbage

I'm trying to build a Connector to Dynamics 365 Business Central but I'm having problems getting the data. Please help me figure out why when I send a GET request using cURL and PHP, it produces the following output:
�S[O�0�+(��&��qB��i�t�F/���=�ا��Ďb�}N�"&�M�����������8�0% �;
Here is my code:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.businesscentral.dynamics.com/{tenantID}/customers/?$filter=displayName%20eq%20'Shawn%20Test'",
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: {Auth Code}"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
It seems like a character encoding issue, or maybe you are receiving binary data.
What is this endpoint supposed to return? Check the response headers, it might give you a clue.
This is an encoding problem, try to remove CURLOPT_ENCODING line from your request or try to choose different encoding from your browser

Php curl request to wit.ai speech return empty response

I am trying to send a Curl request to Wit.ai speech.
I am recording a .wav file on the frontend with recorder.js.
I specified mono channel, since wit.ai only supports mono.
I know that php7 doesn't support # for CURLOPT_POSTFIELDS, so I downgraded my php to 5.6 just to keep it simple.
I used Postman to generate the Curl request.
I also saw this Post which his similar to my issue but I am using a file: How to stream speech to wit.ai speech end point
Note: What I think, I am still lacking wav file parameters, but I am not sure how to get them. I tried properties but there wasn't much info.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.wit.ai/speech?v=20170307",
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_SAFE_UPLOAD => false,
CURLOPT_POSTFIELDS =>"#uploads/audio1585176568858.wav",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer $token",
"Content-Type: audio/wav"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response
The response I get back is this : { "_text": "", "entities": {} }.
I also used a python script with https://pypi.org/project/SpeechRecognition/ lib with same audio .wav file and it worked.

CRM show data using PHP CURL

I want to show my data on my CRM using PHP with curl and also using API.
If there is any other method. You can kindly show it to me
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://b.maxmind.ma/api/leads",
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(
"authtoken: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyfdfdiOjE1ODEwODk2MjJ9.Selc36cmT3XyXH6cGdJ3SN-332kx7"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
the response show : error code: 1020
I think the problem is that the website you are trying to request is protected by Cloudflare.
When i try by the postman , this is the header response a get :
The error code 1020 you got is from cloudflare .
First, you can try using this solution that bypasses the Cloudflare problem
KyranRana/cloudflare-bypass
If not working, in this case, you must contact the site owner to remove the block (more here)

Categories