curl example of file download doesn't work - box-api

If I follow the box api 2.0 curl example to get the metadata of a file, I get the information. When I append /data to the url to download the file, curl says:
curl: (52) Empty reply from server
I did this with two files, both text files, same result.
The system here is OpenBSD 5.0.
In python 3, the same request raises an exception that says that the reply from the server contained a status line that is empty.

I had the same issue. The problem is that you need the CURLOPT_FOLLOWLOCATION option set to true. Box will execute a redirection from the API call to the actual file. Without it, it will return an empty response.

An empty response should be returned in the event that the file itself is empty. If, for instance, the two text files don't have any text in them, no text will be returned i.e. this appears to be expected behavior.

I'm getting the same issue. I have uploaded a file successfully, retrieved the file ID, then when I try to download the file, nothing happens. Like the original poster mentioned, if I take off the "/data" portion of the URL, then I can get all of the info back about the file successfully.
Here is my "download code" that gets called from a form with a simple "Download File" button for that particular file ID.
$boxkey = "ThisIsMyAPIKey";
$auth_token = $_POST['auth_token'];
$url = "https://www.box.com/api/2.0/files/".$_POST['file_id']."/data";
$header = array("Authorization: BoxAuth api_key=".$boxkey."&auth_token=".$auth_token);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_exec($ch);
curl_close($ch);

$url = "https://api.box.com/2.0/files/$fileId/content?access_token=$accessToken";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);

Related

CURL returning different responses

My code is this:
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-type: application/json','Content-Length: ' . strlen($content), 'Cache-Control: no-cache'));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
if($result===FALSE)
{echo "cURL ERROR:".curl_error($ch);}
echo $result;
The problem is the fact that sometimes it works and sometimes it doesn't.
When it works I get a valid JSON response, like:
Working,
but when it doesn't, I get this:
Not Working
I should mention that I don't have access to the API server. Can anyone help me with any idea? Thank you!
The issue was that I was using a minification plugin that after a while was breaking the jQuery ajax call.
I solved it by implementing an inline script for the ajax call, using just javascript (no jQuery library dependency).

request a web service from laravel that returns a json object

I have this Url : https://ipfind.co/?ip=188.225.179.138&auth=dde7cf52-2294-47e3-adb2-1e559099527e
when I request it from browser it gives me this response:
{"ip_address":"188.225.179.138","country":"Palestinian Territory, Occupied","country_code":"PS","continent":"Asia","continent_code":"AS","city":"Ramallah","county":null,"region":null,"region_code":null,"timezone":"GMT+2","owner":"COOLNET NEW COMMUNICATION PROVIDER","longitude":35.2,"latitude":31.9}
how can I invoke this url from laravel?
note that I tried this code, but it didn't work.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://ipfind.co/?ip=188.225.179.138&auth=dde7cf52-2294-47e3-adb2-1e559099527e');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec();
curl_close($ch);
dd($data);
Thanks in advance.
In the Curl you shall do this Easy 5 steps
Step 1
Initiate the Curl $cSession = curl_init();
Step 2
Define the Curl Url
curl_setopt($cSession,CURLOPT_URL,"https://ipfind.co/?ip=188.225.179.138&auth=dde7cf52-2294-47e3-adb2-1e559099527e");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
Step 3
Execute the Curl
$result=curl_exec($cSession);
Step 4
Close the Curl Connection
curl_close($cSession);
Step 5
Print the Result
echo $result;
Putting all together
<?php
$cSession = curl_init();
curl_setopt($cSession,CURLOPT_URL,"https://ipfind.co/?ip=188.225.179.138&auth=dde7cf52-2294-47e3-adb2-1e559099527e");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
$result=curl_exec($cSession);
curl_close($cSession);
echo $result;
?>
There is a PHP package for getting an IP address for IP Find that you can install with composer here that works great with Laravel.
https://github.com/tenfef/ipfind-php/

What does this Microsoft error message mean?

I went to Microsoft developer website and gained a Client ID and Client Secret.
And the following is my source code. I replaced the last two digits with XX
<?php
$ClientID='000000004C12B0XX';
$ClientSecret='yAFqUbNwCqSbs-dQJ5BzF1tWBZggFWXX';
$ClientSecret = urlencode($ClientSecret);
$ClientID = urlencode($ClientID);
// Get a 10-minute access token for Microsoft Translator API.
$url = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13";
$postParams = "grant_type=client_credentials&client_id=$ClientID&client_secret=$ClientSecret&scope=http://api.microsofttranslator.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$rsp = curl_exec($ch);
print $rsp;
?>
And the error message I am getting is
{
"error":"invalid_client",
"error_description":"ACS50012: Authentication failed.
Trace ID: e2f0bfde-d086-4900-ba1f-a58f02693dac
Correlation ID: 3750d010-2e9f-45c5-8ba0-f28dfc050909
Timestamp: 2014-10-20 17:20:26Z"
}
I know, it is very old question, but Microsoft has so many ID's.. I also was confused with "invalid_client" error. Microsoft means Client ID is a value, pointed on https://datamarket.azure.com/developer/applications in "Client ID" column. It is an ID you pointed when created your application. Client ID can be any string, e.g. "myid" is a valid Client ID.
$ClientID='000000004C12B0XX';
Looks like generated value, not user pointed.

curl request to google places api returns "bool(false)"

I've tried using the Javascript version of google places API, but now I'd like to move the code to server side with an API key. I've tried using all possible permutations and combinations of the curl request, but couldn't find a solution to it.
$url = urlencode('https://maps.googleapis.com/maps/api/place/autocomplete/json?input=' . $input . '&types=establishment&location=13.052415, 80.250824&radius=500&sensor=true&key=KEY');
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
NOTE: I've used my app's KEY in the "key" params.
Response I get => bool(false)
Any suggestions?

Post data using curl without showing the redirect URL in adress bar

Below is the my curl for senting some parameters to a url.But the parameter I m senting has sensitive datas like passwords and other things.But during posting the datas will be showed in the adress bar as redirect url.How to avoid this?.What to do to sent data without showing it in address bar?
$parameters='MerchantId='.$merchantId.'&Password='.$Password.'&ReferenceNo='.$ReferenceNo.'&RemoteIP='.$RemoteIP.'&Amount='.$Amount.'&BankId='.$BankId.'&Checksum='.$checksum.'&Name='.$Name.'&MobileNo='.$MobileNo.'&Email='.$Email;
//set POST variables
$url = 'https://payment.essecom.com/NetBanking/PayDirekt.jsp?'.$parameters;
//header('Location: '.$url);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: text/plain', 'Content-length: 900'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
echo curl_exec($ch);
curl_close($ch);
}
It's pretty easy to not include headers in output. Just add option CURLOPT_HEADER and set it to false. But without headers how can you be sure that request is completed successfully?