What is the php curl version of the below curl command - php

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);

Related

PHP Dropbox API download file to server

I'm attempting to download file from dropbox to my server, nothing is returned but i know the file exists
$headers = array('Authorization: Bearer '.$access_token,
'Content-Type: application/json',
'Dropbox-API-Arg: {"path": "'.$filepath.'"}');
$file = fopen($filename, "w+");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://content.dropboxapi.com/2/files/download");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FILE, $file);
$result = curl_exec($ch);
curl_close ($ch);
fclose($file);
Where did i go wrong? How do i solve?
try this way and check the output of the result. and save it to the local server.
$url = 'https://content.dropboxapi.com/2/files/download';
$formData = [
"path"=> $filepath
];
$ch = curl_init($url);
$authorization = "Authorization: Bearer ".$access_token;
$request_data = "Dropbox-API-Arg: ".json_encode($formData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorization, $request_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
dd($result)

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);

How to use curl in php,post json data and download file stream

follow codes is php code ,but it don't download anything.
$postdata=$jsonstr;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
//'Content-Type: application/x-www-form-urlencoded"',
'Content-Length: ' . strlen($postdata)
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata );
$output = curl_exec($ch);
$info = curl_getinfo($ch);
echo "<br/>-------------------<br/>";
//print_r($output);
echo "<br/>-------------------<br/>";
mySaveFile($output, "./file.tmp");
curl_close($ch);
I get 'file.tmp' always is size 0.I suspect I set up a mistake for CURLOPT_HTTPHEADER,
thanks for everyone help.

how to POST CURL with data and headers in 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.

Curl PHP errors with rackspace api

I'm trying to generate a token with the "Cloud Identity" API from Rackspace(https://developer.rackspace.com/docs/cloud-identity/v2/developer-guide/#generate-an-authentication-token)
This is the request i need:
$ curl https://identity.api.rackspacecloud.com/v2.0/tokens \
-X POST \
-d '{"auth":{"RAX-KSKEY:apiKeyCredentials":{"username":"yourUserName","apiKey":"yourPassword"}}}' \
-H "Content-type: application/json" | python -m json.tool
And this is how I'm trying to do it:
<?php
$url = 'https://identity.api.rackspacecloud.com/v2.0/tokens';
$apiKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$data = "'{\"auth\":{\"RAX-KSKEY:apiKeyCredentials\":{\"username\":\"XXXXXXXX\",\"apiKey\":\"$apiKey\"}}}'";
$headers = array();
$headers[0] = '"Content-Type: application/json" | python -m json.tool';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE);
$response = curl_exec($ch);
$info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo($info);
?>
And I get this: 415 {"unsupportedMediaType":{"code":415}}
When I change the header from
$headers[0] = '"Content-Type: application/json" | python -m json.tool';
to
$headers[0] = 'Content-Type: application/json | python -m json.tool';
I get this
And finally when I change the header to this one:
$headers[0] = 'Content-Type: application/json';
i get this error code: 400 {"badRequest":{"code":400,"message":"Invalid json request body"}}
Am I doing it right?
Thanks in advance.
Is there any reason why you're not using the PHP SDK? It makes doing things like this a lot easier.
You'd need to change your PHP to:
<?php
$url = 'https://identity.api.rackspacecloud.com/v2.0/tokens';
$username = 'foo';
$apiKey = 'bar';
$data = <<<EOT
{"auth": {"RAX-KSKEY:apiKeyCredentials":{"username":"$username","apiKey":"$apiKey"}}}
EOT;
$headers = array('Content-Type: application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE);
$response = curl_exec($ch);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE);

Categories