how to POST CURL with data and headers in PHP - php

i would like to make a CURL to get an auth tokken.
The platform dev forum give me that:
curl -X POST \
--header 'Content-Type: application/json; charset=utf-8' \
--header 'Accept: application/json' \
-d '{"email":"MY_EMAIL","password":"MY_PASSWORD"}' \
'https://api.voluum.com/auth/session'
How do i make that work in PHP?

Try this: https://incarnate.github.io/curl-to-php/
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.voluum.com/auth/session");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"email\":\"MY_EMAIL\",\"password\":\"MY_PASSWORD\"}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/json; charset=utf-8";
$headers[] = "Accept: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

$vars = '{"email":"MY_EMAIL","password":"MY_PASSWORD"}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.voluum.com/auth/session");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = ['Content-Type: application/json; charset=utf-8',
'Accept: application/json'];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
exit;
}
curl_close ($ch);
print_r($server_output);

You can do it like below:-
<?php
$data_string = '{"email":"MY_EMAIL","password":"MY_PASSWORD"}';
$ch = curl_init('https://api.voluum.com/auth/session');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Accept: application/json'
));
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
exit;
}
curl_close ($ch);
var_dump($result);
I run it and found below response (because i don't have mail-id and password):- https://prnt.sc/gdz82r
But the pleasant part is code executed successfully and when you will provide right credentials then it will give you correct output.

Related

PHP: curl script fails

The task seemed simple.
The login (email) used for authentication must be passed in the login request parameter. In the request body (Body), the user password is passed as a string encoded in UTF-8.
Example request:
POST /auth/authenticate-by-pass?login=testlogin#testDomain.net HTTP/1.1
Host: somehost.ru
Body: somepassword
Cache-Control: no-cache
If the request is successful, the response will contain a JSON object
Tried to do like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://somehost.ru/auth/authenticate-by-pass?login=mylogin#mydomain.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, "mypassword" );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain', 'Host: somehost.ru'));
$result=curl_exec($ch);
$php_obj = json_decode($result, true);
print_r($php_obj);
No result. Nothing is displayed. Please help.
In my understanding what you need can be simply (please amend the headers to further suit your needs if necessary) :
$ch = curl_init();
$post = [
'password' => 'xxxxxx'
];
curl_setopt($ch, CURLOPT_URL, 'http://somehost.ru/auth/authenticate-by-pass?login=mylogin#mydomain.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
$php_obj = json_decode($result, true);
print_r($php_obj);
curl_close($ch);
Solved the problem. Ken Lee and everyone else thanks for the help.
$ch = curl_init();
$post = 'mypassword';
curl_setopt($ch, CURLOPT_URL, 'http://somehost.ru/auth/authenticate-by-pass?login=mylogin#mydomain.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
$php_obj = json_decode($result, true);
print_r($php_obj);
curl_close($ch);

Curl returns empty when response is too big

I am sending curl request like
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.example.com/Search');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{ \"apikey\": \"xxxxxxxxxxxxxxx\",\"min\": 20, \"items_per_page\": 5000 }");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/json';
$headers[] = 'X-Requested-With: XMLHttpRequest';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
Now, when "items_per_page" parameter is set to under 4000 it works well everytime, but when its default 5000 it returns empty response.
I tryed to find the error, but its doesn't return anything
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_getinfo returns http_code 504.
I also tryed adding:
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10000 );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
etc. but still can't make it work.

What is the equivalent curl request in PHP

I'm trying to get in PHP this curl:
curl -X POST -u "apikey:MY_API_KEY" \
--header "Content-Type: text/plain;charset=utf-8" \
--header "Accept: application/json" \
--data-binary "MY_TEXT" \
"https://MY_DIRECTION"
so far I came up with this:
$curl = curl_init();
$post_args = array('data-binary' => $MY_TEXT );
$header_args = array(
'Content-Type: text/plain;charset=utf-8',
'Accept: application/json'
);
$apiKey = '$MY_API_KEY';
$api_args = array('apikey: ' . $apiKey);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $api_args);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_args);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header_args);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_URL, $MY_DIRECTION);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
curl_close($curl);
json_decode($result, true);
I'm trying to use Personality Insights service of IBM.
Let's try with this-
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://MY_DIRECTION');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "MY_TEXT");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'apikey' . ':' . 'MY_API_KEY');
$headers = array();
$headers[] = 'Content-Type: text/plain;charset=utf-8';
$headers[] = 'Accept: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

What is the php curl version of the below curl command

What would be the php curl version of the below command
curl -X PUT -H 'Content-Type: text/csv' -H 'Accept: application/json' -d #file.csv [URL]
I have the below php curl version which doesn't work
$headers = [
'Content-Type: text/csv',
'Accept: application/json'
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($filename));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
Try below code
$headers = [
'Content-Type: text/csv',
'Accept: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ''.$url'');
curl_setopt($ch, CURLOPT_PUT, 1);
$fp = fopen($file_path, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response= curl_exec ($ch);
fclose($fp);
There is a tool to convert cURL command to PHP version of cURL. You can try this website: curl-toPHP.
This is the example output using your cURL command.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'your-post-remote');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$post = array(
'file' => '#' .realpath('file.csv')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
$headers = array();
$headers[] = 'Content-Type: text/csv';
$headers[] = 'Accept: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

Create object with data from cURL

So i'm using cURL with PHP to retrieve some data from a JSON file. The cURL script returns something like this: https://s4.postimg.org/43svl8rot/image.png
I'm using this PHP script:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://test.pt/api/objgroupinfo/16Jcr05g37KpLklz");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "X-Apikey: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://test.pt/api/dataout/IAfhAfTIUZrCje5q.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "X-Apikey: xxxxxxxxxxxxxxxxxxxxxx";
$headers[] = "X-Startdate: 2016-10-01 00:00:00";
$headers[] = "X-Enddate: 2016-10-10 15:00:00";
$headers[] = "X-Channelnum: 0";
$headers[] = "X-Reclimit: 200";
$headers[] = "User-Agent: test/1.0";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
$json = json_decode($result,true);
print_R($json);
curl_close ($ch);
Can someone help me on how to turn this data into an object or array that i can use? I need to acess this data to show it graphically
json_decode will convert a JSON encoded string to a PHP variable, see json_decode. It looks like your code is already doing this.

Categories