Return a file in my controller that is downloaded from an API - ajax

I would like to return a file in my controller that is downloaded from an API (who needs a basic auth).
This API returns the file and the problem is I can't use ajax because of Access-Control-Allow-Origin, even when I add the basic auth in the header, it returns this error. And I don't have access to this API so I can't change it the config.
So I found another way! I call this API from my controller with the header and the basic auth. It's working! I can access this API and I don't have the error message: Access-Control-Allow-Origin.
But the problem is the API returns heavy files, and I have this exception OutOfMemoryException. It's normal.
So I would like to returns in my controller just a redirect link to this API with the header and basic auth to download the file. Do you think it's possible?
An example of what I want (But it's not working):
public function indexAction()
{
// API
$url = "http://api-who-callback-a-file.com/api/file";
//Create header
$curl = curl_init();
$url = sprintf("%s?%s", $url, http_build_query(false));
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "admin:admin");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Get file (Problem here because the file is to heavy to be downloaded by the server)
$result = curl_exec($curl);
curl_close($curl);
// Return the file
return $this->file($result);
}
In this example, it's not working because the server download the file and it's too heavy. Me I just want to return the URL with the Header to the client, and he will download the file.
Thank you

Related

How to send multiple files under the same name using CURL in php7+

Hi am trying to send multiple files in a php Curl operation. I am using PHP7.2 and trying to send 15 images under the same key.
If I am trying to send just 1 file its working just fine.
$post["image"] = new \CurlFile('image_full_path.png', 'image/png', 'file.png');
But when I am trying to put multiple files its not working anymore.
Already tried
$post["image[0]"] = new \CurlFile('image_full_path.png', 'image/png', 'file.png');
And
$post["image_0"] = new \CurlFile('image_full_path.png', 'image/png', 'file.png');
Does not work
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"url");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
$path = '/var/www/download/download.pdf';
file_put_contents($path, $response);
echo $response;
I believe you just need to create an associative array to send multiple images with the same key.
$post["image"] = [
new \CurlFile('image_full_path1.png', 'image/png', 'file1.png'),
new \CurlFile('image_full_path2.png', 'image/png', 'file2.png'),
];
It seems like the issue is with my destination of the request. The end point of the curl is written on Python Flask and it seems like if we send it like we do in php, flask dont understand it.
So in php
image[0] => 'image1.png'
image[1] => 'image2.png'
Can be valid and we will get both the files under image key in the request,
in python they are coming in different keys like
image[0] and image[1].
If we are tring to look for image in the request it will give an error, but image[0] and image[1] as a string are working fine.
The solution was to send the files as base 64encoded url in an associative array.
P.S. I am not an expert in Python, so my explanation can be wrong. If some one knows python and is aware if this issue please comment here.
Thank you.

Courses.List displays course, but Courses.Delete says Course Unknown

I am trying to delete all classes from Google Classroom using the API. I call Classes.List to retrieve the ID number of all classes, then pass this to Classes.Delete. However at this point I get a 'Requested entity was not found.' error message.
I am unsure how this returns in the List command but fails on Delete. Are there any prerequisites of the Delete command that I cannot see on the Google documentation?
Same happens on the 'Try This API' section of the Google website, so fairly confident it isn't a code issue.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
"https://classroom.googleapis.com/v1/courses");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer $token",
"Content-Type: application/json"
));
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response, true);
foreach ($response['courses'] as $Course)
{
$VLEID=$Course['id'];
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL,
"https://classroom.googleapis.com/v1/courses/".$VLEID);
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch1, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer $token"
));
$response1 = curl_exec($ch1);
curl_close($ch1);
var_dump($response1);
echo "<br/>";
die();
}
This seems to be bug on Google classroom side. Please see issue logged in GC issue tracker for this around the same time this question was posted here and the issue creator also mentioned it could be regressed issue as same API was returning different number of courses earlier.
https://issuetracker.google.com/issues/134993210
I'm seeing the same problem in my automated test suite. As far as I can tell, some of the courses being returned from Courses.List is returning courses that have already been deleted. I can't do a Course.Get on the id's either, nor do they show in when browsing courses on the Classroom web page. I know it didn't used to do that because this test suite has been around for years, but I hadn't run it recently, so either there has been a regression somewhere on the Google side, or perhaps it just take a while for the delete to fully propagate. I know the distributed nature of some of these services sometimes manifests in (usually) short term inconsistencies.

How to integrate third party api in my site magento

I am using Magento ver. 1.9.2.1.
I don't have any idea to integrate third party api in magento. I have to integrate the api for website.com, for that they provide the login details and a doc.
In that doc, it is mentioned to integrate
1. Search Service Availability API
2. Order Creation API
3. Tracking API
For each above API, they provide code,details and json response.
I have created a user and role from admin. And create a file test.php in root folder and put their code and checked in browser, but it shows blank
Given code in doc for Search Service Availability API:
<?php
ob_start();
error_reporting( 0 );
ini_set('display_errors', 'off');
$pickup_pincode = ‘110001’ // pass pincode
$delivery_pincode =’110001’; // Pass the Parameters Here pincode
$service=’Online’ // Cod or Online
$weight=’1’ // pass weight
$service_name = ‘Standard’ // standard / Priority / Economy
$request_url ='http://avnbiz.co.in/test/AVNBIZ/webservice/test_search_service.php';
$post_data =
'&pickup_pincode=$pickup_pincode&delivery_pincode=$delivery_pincode&weight=$w
eight&service=$service&service_name=$service_name'';
$post = curl_init();
curl_setopt($post, CURLOPT_URL, $request_url);
curl_setopt($post, CURLOPT_POST,TRUE);
curl_setopt($post, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($post, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($post);
curl_close($post);
print_r($response);
$result = json_decode($response, true);
print_r($result);
?>
response
[{"Service Name":"Standard","partner_id":"2","partner_name":"DOT
ZOT","price":"0","status":"PRICE NOT AVAILABLE "},{"Service
Name":"Standard","partner_id":"9","partner_name":"GATI","price":"0","status":"PRICE
NOT AVAILABLE "},{"Service
Name":"Standard","partner_id":"324","partner_name":"Fedex TSCI Pvt
Ltd","price":"50","status":"Success"},{"Service
Name":"Standard","partner_id":"327","partner_name":"GATIKWE EXPRESS PVT
LTD","price":"50","status":"Success"},{"Service
Name":"Standard","partner_id":"335","partner_name":"demo","status":"NO SERVICE
AVAILABLE"}]
If anyone has any idea, please let me know where I am making a mistake.

How to add segment that is a certain subgroup using Mailchimp API v3

I want to create a subgroup that is a subgroup, using API v3, and I'm not sure how to set the condition in the request. For some reason I can't get to the Playground today, so I can't play with it.
I'm thinking something like:
function createSegment($apikey, $dc, $list_id, $name) {
$data = array(
"name"=> $name,
**CONDITION**
);
$data = json_encode($data); // API requires JSON objects
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://".$dc.".api.mailchimp.com/3.0/lists/".$list_id."/segments");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true); // declare request is POST type
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // set POST data
curl_setopt($ch, CURLOPT_USERPWD, "user:".$apikey); // HTML Basic Auth
$output = curl_exec($ch); // execute and capture response
curl_close($ch);
}
This is what the API Support had to say:
With the release of version 3.0 of our API we made some changes to the structure of how we use IDs. In this case there are currently 2 sets of IDs that groups can be referenced by, depending on which version of the API you are using.
In version 2.0, the group ID is an integer and can be retrieved using the /lists/interest-groupings call here:
https://apidocs.mailchimp.com/api/2.0/lists/interest-groupings.php
With version 3.0, the group ID is a hash and is returned by making a GET request to the Lists Interest Collection endpoint here:
http://kb.mailchimp.com/api/resources/lists/interest-categories/interests/lists-interests-collection
When creating a segment however, we currently only accept the group ID returned in version 2.0 of the API, regardless of whether that segment call is being made with 2.0 or 3.0. For this reason, when segmenting by groups, a call would need to be made to the 2.0 endpoint to retrieve those groups IDs. These IDs are static though, so if they are expected to be updated, they would only need to be requested once and can then be stored locally.

Curl Delete function dumps json to browser - how to fix

The following function transfers a curl delete request to an api, and returns the response to php.
The api returns a json array for all requests (get, post, put and delete), and everything works fine for everything except delete requests.
The following curl function doesn't seem to be working properly:
function curl_delete($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_URL,$url);
$result = curl_exec($ch);
curl_close($ch);
return $result; // <-- Function ALWAYS returns whether this is here or not??!..
}
This is how I call the function from PHP:
// [DELETE API URL]:
$url = 'http://localhost/website/api/users/user/id/123';
$html = json_decode(curl_delete($url), true);
If you look at the function (above), whether or not I include the return statement (at the end of the function), the result of the curl call (the jsonified array) always gets dumped to the browser after the function has completed - which is not what I want.
If I then say echo count($html), the correct length of the returned result will print, and the $html array is working fine, however I can't seem to prevent it from automatically dumping to the screen.
This does not happen for any of the other curl functions, which just work as expected.
QUESTION:
Is this normal behaviour? How do I prevent the json from dumping to the screen?
PS Using Codeigniter & Phil Sturgeon's REST Server
You should try Phil Sturgeon's cURL library for Codeigniter and its method simple_delete() :
$this->load->library('curl');
$url = 'http://localhost/website/api/users/user/id/123';
$response = $this->curl->simple_delete($url);
$html = json_decode($response, TRUE);
If it does not solve your problem, maybe you will have to take a closer look at your REST server.

Resources