Translating cURL command to PHP - php

I'm trying to translate the following cURL command to PHP.
curl -k https://api.box.com/2.0/files/content -H "Authorization: Bearer 8tuuIMsrf3ShyODVdw" -F filename=#txt.txt -F folder_id=0
Here is my attempt:
$url = 'https://api.box.com/2.0/folders/content';
$params['filename'] = '#txt.txt';
$params['folder_id'] = '0';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer ".$this->access_token, "Content-Type: multipart/form"));
$data = curl_exec($ch);
curl_close($ch);
It's not working however. Can anyone help me understand why what I'm doing is wrong and what I can do to modify my code? Thanks!!

Try getting rid of the header "Content-Type: multipart/form" from your CURLOPT_HTTPHEADER option as cURL will take care of this for you based on your post data.
Since you are uploading a file, the content-type will automatically be set to multipart/form-data which is actually the correct content type, not multipart/form.

curl -k means insecure. Add the following line:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
Translated from the german manual (Note that the english version is missing this information):
If CURLOPT_SSL_VERIFYPEER has been deactivated, the option CURLOPT_SSL_VERIFYHOST has to be disabled as well.
The rest of your code should work fine.

Related

Twitter API token request returning gobbledygook

I'm trying to use Application Only Authentication, as described here:
https://developer.twitter.com/en/docs/basics/authentication/overview/application-only
I'm using the following PHP code to do so.
if(empty($_COOKIE['twitter_auth'])) {
require '../../social_audit_config/twitter_config.php';
$encoded_key = urlencode($api_key);
$encoded_secret = urlencode($api_secret);
$credentials = $encoded_key.":".$encoded_secret;
$encoded_credentials = base64_encode($credentials);
$request_headers = array(
'Host: api.twitter.com',
'User-Agent: BF Sharing Report',
'Authorization: Basic '.$encoded_credentials,
'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
'Content-Length: 29',
'Accept-Encoding: gzip'
);
print_r($request_headers);
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, 'https://api.twitter.com/oauth2/token');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
$attempt_auth = curl_exec($ch);
print_r($attempt_auth);
}
It should return JSON with the token in it, but instead it returns gobbledygook, as seen in the image below:
I'm sure I'm missing some very simple step, where am I going wrong?
If I send the curl request without the headers, it returns an error in JSON format as expected, so is there something wrong with my headers?
You have few options here. Instead of setting header directly, use below
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
If you set header directly then you should use
print_r(gzdecode($attempt_auth));
See below thread as well
Decode gzipped web page retrieved via cURL in PHP
php - Get compressed contents using cURL

I can't figure out why this cURL request won't work in PHP

Edit: The issue is that when I use var_dump(), it's null.
Sorry if I do this wrong, I'm new here. What I know:
The API works with normal cURL (curl -H "Authorization: [token]" -d "src=google" -d "dest=https://google.com")
$src, $dest, and $auth are set (and correctly).
When entering an invalid $auth, the API returns a 401 (Unauthorized Request), along with all failed requests.
The API is my own (also built in PHP).
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://coles.life/api/long/create/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$auth));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(
array(
"src" => $src,
"dest" => $dest
)
));
$out = curl_exec($ch);
curl_close($ch);
$res = json_decode($out, true);
add
curl_setopt($handle, CURLOPT_POST, true);
to define that this will be a POST request.
I fixed it, turns out in the database I had varchar(255) set and was trying to pass a value > 255.

Convert cURL to be used in php function

Currently im working on box platform, and trying to upload a file to the box server. Box uses cURL to upload files, and i'm trying to send cURl requests from php. So far i've converted most of the cURL commads to php jargon, but i could't figure out how to pass in the attributes(name, path, containing folder) of the file to be uploaded.
here is the cURL
curl https://upload.box.com/api/2.0/files/content -H "Authorization: Bearer APP_USER_TOKEN" -X POST -F attributes='{"name":"Jon_Snow.jpeg", "parent":{"id":"0"}}' -F file=#Jon_Snow.jpeg
and here is the php version for the incomplete cURL command.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://upload.box.com/api/2.0/files/content");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Authorization: Bearer ".json_decode($accessToken, true )['access_token'];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
How do i do the last part of the cURL command which is
-F attributes='{"name":"Jon_Snow.jpeg", "parent":{"id":"0"}}' -F file=#Jon_Snow.jpeg
Edit: the suggestion of 'possible duplicate' is not accurate, i am asking of a way to add attributes to the uploaded files in the form -F attributes='{"name":"Jon_Snow.jpeg", "parent":{"id":"0"}}' i dont see how the suggested answer is relevant
Found the answer, i needed to json_encode the attributes array and then use new \CURLFile() function to create a file handle, it did NOT work with realpath()
$attributes = array('name'=>$fileName,'parent'=>array('id'=>$folderId));
$file = new \CURLFile($filePath);
$fields = array('attributes' => json_encode($attributes), 'file' => $file);
$headers = array("Authorization: Bearer ".$accessToken);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uploadUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
Try this: art about sending files via post and curlib
-F option means you're sending a post Field with name and content. Content in this case is from File becasue you're using # prefix

Zoopla Sandbox with cURL http header error

I'm developing code for an estate agent to upload properties to Zoopla via their datafeed
I'm having problem adding a required profile to the http header that is required
The only example in the documentation is this test from linux:
echo '{ "branch_reference" : "test" }' |
curl --key /path/to/private.pem --cert /path/to/certificate.crt --data #-
--header "Content-type: application/json;
profile=http://realtime-listings.webservices.zpg.co.uk/docs/v1.1/schemas/listing/list.json"
https://realtime-listings-api.webservices.zpg.co.uk/sandbox/v1/listing/list
I'm having a problem adding the profile to the http header in cURL
Here's my code:
$json['branch_reference']="test";
$json=json_encode($json);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSLKEY,'private.pem');
curl_setopt($ch, CURLOPT_SSLCERT,'zpg_realtime_listings_1468337696150754_20160712-20260710.crt');
curl_setopt($ch, CURLOPT_URL, "https://realtime-listings-api.webservices.zpg.co.uk/sandbox/v1/listing/list");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'profile: http://realtime-listings.webservices.zpg.co.uk/docs/v1.1/schemas/listing/list.json'
));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
if(curl_error($ch)){echo "CURL ERROR ".curl_error($ch)."<br>";}
$result = curl_exec($ch);
curl_close($ch);
echo $result;
The response I'm getting from Zoopla is:
{ "error_advice" : "The Content-Type header is missing the 'profile' parameter. Please supply 'profile', specifying the schema URL for the method you are calling: /sandbox/v1/listing/list", "error_name" : "missing_profile" }
Can anbody please advise on the correct way to add the profile to the header?
Thanx
From their example, it looks like you should be passing the whole string as a single Content-Type HTTP header, rather than two separate ones:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; profile=http://realtime-listings.webservices.zpg.co.uk/docs/v1.1/schemas/listing/list.json'
);

Neo4j using Traverser via REST HTTP

I'm trying to use Neo4js Traverser via the HTTP API.
If I use it via curl on the command line it works fine, but when I try to use it via curl through PHP I get an error all the time.
This is curl command:
curl -H Accept:application/json -H Content-Type:application/json -X POST -d '{"order":"depth first"}' http://localhost:7474/db/data/node/5/traverse/node
And this is my PHP Code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:7474/db/data/node/5/traverse/node");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept:application/json',
'Content-Type:application/json'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, '"{"order": "depth first"}"');
$output = curl_exec($ch);
echo '<pre>';
var_dump(curl_getinfo($ch));
var_dump($output);
curl_close($ch);
This is the error I get:
HTTP ERROR 500
Problem accessing
/db/data/node/5/traverse/node. Reason:
java.lang.String cannot be cast to java.util.Map
Any Ideas?
Looks like you have quotes before the JSON string:
curl_setopt($ch, CURLOPT_POSTFIELDS, '"{"order": "depth first"}"');
Might want to try this:
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"order": "depth first"}');
EDIT: Although better yet, I'd use json_encode with an associative array to ensure proper escaping if necessary:
$json_data = array("order" => "depth first");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($json_data));

Categories