Unable to upload a file to foldergrid using API - api

Trying to use folder grid api to upload a file. There are 2 steps.
Calling provision a file to get a location with a guid.
Uploading the actual file using a PUT.
I am able to do the first call and get a location of the type https://files.foldergrid.com/upload/xxx. I then try to upload the file using this as my location and keep getting
< HTTP/1.1 100 Continue
* Empty reply from server
* Connection #0 to host files.foldergrid.com left intact
* Closing connection #0
from the server not sure what I am missing?
Here is the code I am using:
After calling the provision api I get back a url which I store in
$url = 'https://files.foldergrid.com/upload/xxx'
and I have the path to the file I want to upload in file
$file = 'some path to file'
then I have call the following method:
public function putFile($file,$url) {
$headers = array(
'fg-eap: false',
'fg-md5:'.md5_file($file),
);
$fh = fopen($file, "rb");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl,CURLOPT_PUT,true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_INFILE, $fh);
curl_setopt($curl, CURLOPT_INFILESIZE, filesize($file));
curl_setopt( $curl, CURLOPT_VERBOSE, true );
$result = curl_exec($curl);
fclose($fg);
curl_close($curl);
return $result;
}

The code you've posted looks to have everything needed except the Content-Type header. Try setting your headers array as:
$headers = array(
'fg-eap: false',
'fg-md5:'.md5_file($file),
'Content-Type: binary/octet-stream'
);
Also, in response to your query here we've posted an updated PHP client sample under our API documentation here.

Related

getList() must be an instance of Magento\Framework\Api\SearchCriteriaInterface

Been trying to figure out what is going on here, but gotten stuck. Anyone got an idea as to what is going on.
Using Magento 2.2.4
( ! ) Fatal error: Uncaught TypeError: Argument 1 passed to Magento\Catalog\Model\ProductRepository\Interceptor::getList() must be an instance of Magento\Framework\Api\SearchCriteriaInterface, string given in C:\wamp64\www\magento2\generated\code\Magento\Catalog\Model\ProductRepository\Interceptor.php on line 85
( ! ) TypeError: Argument 1 passed to Magento\Catalog\Model\ProductRepository\Interceptor::getList() must be an instance of Magento\Framework\Api\SearchCriteriaInterface, string given in C:\wamp64\www\magento2\generated\code\Magento\Catalog\Model\ProductRepository\Interceptor.php on line 85
Code I try to execute:
set_time_limit(0);
define('TOKEN', 'token code');
define('URL', 'http://localhost:8080/magento2/index.php');
$headers = array("Authorization: Bearer ". TOKEN);
//API URL to get all Magento 2 modules
$requestUrl = (URL . "/rest/V1/products?searchCriteria=");
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result);
You need to pass search Criteria while requesting API. Try below code.
set_time_limit(0);
define('TOKEN', 'token code');
define('URL', 'http://localhost:8080/magento2/index.php');
$headers = array("Authorization: Bearer ". TOKEN);
//API URL to get all Magento 2 modules
$requestUrl = (URL . "/rest/V1/products/?searchCriteria[filter_groups][0][filters][0][field]=category_gear&searchCriteria[filter_groups][0][filters][0][value]=86&searchCriteria[filter_groups][0][filters][0][condition_type]=finset");
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result);

cURL post with PHP failing

Using Postman, I have been able to successfully create an API call to retrieve information from the webserver.
However, I have been unsuccessful in doing the same with PHP. Following are a few screenshots from Postman which I am trying to replicate in PHP.
This is my PHP code -
$filters = array("1234", "28");
$selectors = array("Brand", "Name");
$body = array('Filter' => array('SKU'=>$filters, 'OutputSelector' => $selectors));
$url = "https://xyz.neto.com.au/do/WS/NetoAPI";
$method = "POST";
$headers = array(
"NETOAPI_ACTION: GetItem",
"NETOAPI_KEY: xxxxxxx",
"Accept: application/json",
"NETOAPI_USERNAME: puneeth"
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
P.S I have deliberately fudged the url and key.
I am trying to follow instructions as outlined here - https://developers.neto.com.au/documentation/engineers/api-documentation/products/getitem
Few things that might be preventing your code from working:
1. Array to string conversion
You have declared the body of the request as an array in $body. You need to JSON-encode it before passing it to cURL:
$body = array('Filter' => array('SKU'=>$filters, 'OutputSelector' => $selectors));
$bodyJSON = json_encode($body);
[...]
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyJSON);
2. HTTPS request
If you're getting an empty response, you'll also need to configure cURL for SSL communication. A quick fix for this is to ignore the remote server certificate validity:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Although this works, see this answer for a better solution.
3. Missing Content-Type
When sending raw postfields, some servers need to know what type of data you're sending. This is done by setting a Content-Type header:
$headers = array(
"Content-Type: application/json",
[...]
);

I am getting 401 ssl required error for likedin rest api while accessing some basic profile fields

I am using rest api to fetch basic profile data from linkedin with php code. I am successfully able to generate access code and access token but I am getting ssl required error whenever I tried to get basic profile using following url
https://api.linkedin.com/v1/people/~?format=json
I followed all steps to make authenticated requests as listed there https://developer.linkedin.com/docs/oauth2
I am using a non ssl url as call back parameter , Is it necessary to use ssl url ? If not then why I am getting this error .
looking for a solution
below is code to get profile fields
$host = "api.linkedin.com";
$path = "/v1/people/~)";
//$arr = array('oauth2_access_token' => $access['access_token']);
$token = $access['access_token'];
$header[] = "Authorization: Bearer ".$token;
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$ch = curl_init();
// endpoint url
curl_setopt($ch, CURLOPT_URL, $host . $path);
// set request as regular post
//curl_setopt($ch, CURLOPT_HTTPGET, true);
// set http version
curl_setopt($ch, CURLOPT_HTTP_VERSION, 'HTTP/1.1');
// set data to be send
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($arr));
// set header
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
// set header
curl_setopt($ch, CURLOPT_CERTINFO, true);
// return transfer as string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_export($response);
My solution was to use https:// instead of just http://

Authentication Errors Trying to Access to the Google Analytics API via http (not using oAuth)

I'm trying to build a simple script to import page view counts for articles published via my CMS. I easily constructed my query using the Google Analytics API query builder which quickly returns the desired result. A scheduled job on my web server will run the query once per day and update and page view counts.
Because I'm only pulling in pageviews, I believe it wasn't necessary to go through the entire oAuth process. This Google account has only one web property and only one profile, so there isn't a routine needed to derive that.
I registered an app and created an API key. I have ensured that Google Analytics is turned on for this profile. Based on my reading of the API, I believe I can pass this key as an http parameter to properly authorize the query.
When I run the query via http, I get an authorization error (401). The query is included below:
https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A[MY ID]&metrics=ga%3Apageviews&start-date=2012-08-09&end-date=2012-08-23&max-results=50&key=[MY API KEY]
I've Googled many examples of this, but they all seemed to implementing a very elaborate (and in my use case unnecessary) authentication routine. But maybe I'm missing something.
Many thanks in advance.
Kris, frustrated Googler
Use this example to fix 401 error http://dumitruglavan.com/ganalytics-class-access-google-analytics-data-api-with-php/
You need to authorize:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'accountType' => 'GOOGLE',
'Email' => $email,
'Passwd' => $password,
'service' => 'analytics',
'source' => ''
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$auth = '';
if($info['http_code'] == 200) {
preg_match('/Auth=(.*)/', $output, $matches);
if(isset($matches[1])) {
$auth = $matches[1];
} else {
throw new Exception('Login failed with message: ' . $output);
}
}
And after authorize send authorization token in headers:
$headers = array("Authorization: GoogleLogin auth=$auth");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

Uploading files to RapidShare using PHP and cURL via their API

I'm trying to upload files to RapidShare using their API and PHP with cURL. So far, I've got this code:
// Get the RapidShare server to upload to
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=nextuploadserver');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$uploadServer = curl_exec($ch);
// Upload the file to RapidShare
$url = 'http://rs' . $uploadServer . '.rapidshare.com/cgi-bin/rsapi.cgi?sub=upload';
$url .= '&login=login';
$url .= '&password=mypass';
$url .= '&filename=' . $filename;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
// Set post fields
$postFields = array('filecontent' => array('#' . $targetDir . '/' . $id));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
$resp = curl_exec($ch);
die($resp);
But the only response I get is: ERROR: Subroutine invalid. (b6ba5d82). If I just do this (basically don't send the file with the request):
// Upload the file to RapidShare
$url = 'http://rs' . $uploadServer . '.rapidshare.com/cgi-bin/rsapi.cgi?sub=upload';
$url .= '&login=login';
$url .= '&password=mypass';
$url .= '&filename=' . $filename;
curl_setopt($ch, CURLOPT_URL, $url);
$resp = curl_exec($ch);
I get this response: ERROR: No files transmitted. (f269d341)
So I'm guessing there's something wrong with the way I'm sending the file via POST.
Anyone know what could be wrong?
Thank you.
Oddly enough, i just hit this problem as well. after trying for an hour or two to find out what's wrong, it turned out to be quite simple and caused by ignoring a single statement in the api document : "GET parameters are ignored if the POST method is used. So if you use the POST method, send ALL parameters via POST." for upload to work, sub=upload should also be sent as a POST parameter rather than GET.
Add curl parameter
curl_setopt($ch, CURLOPT_INFILESIZE,(string)filesize($filename));
curl_setopt($ch, CURLOPT_INFILE,fopen($filename,'r'));
and $filename must write full path.
Sorry Im english very little.