How to gzip XML file? - php

I am working on integrating my application with the API of another application. Previously I have done such integrations using JSON and had no problem, unfortunately, the application I want to connect to uses XML which I have not interacted with before.
The task is to load an .xml file into a PHP script, then perform gzip on that XML, and then encode the byte array to base64.
I have the XML file ready, I can use base64_encode() too, but I sit on this gzip for several hours and nothing works. (I load the XML file into the script using simplexml_load_file)
I tried using gzencode, gzcompress but it doesn't work.
Authentication involves combining HTTP parameters and encoding them with urlencode(). The req_sig (signature) parameter contains the encoded parameters (mentioned above) + API code. If everything matches then a response is returned, if not then an error is displayed: ERROR4000Signature does not match, even though the signature matches, because it is generated the same way in other queries using GET.
My code:
$xml=simplexml_load_file("request.xml") or die("Failed to load");
$gzip = gzencode($xml);
$base64 = base64_encode($gzip);
$username = 'test-app-user';
$req_id = rand(10000000000, 99999999999);
$api_token = "za5rth23ku776fdvbn2";
$string_bazowy = "command=$base64"."req_id=$req_id"."username=$username";
$encoded = urlencode($string_bazowy);
$string_zmieniony = "$encoded"."$api_token";
$md5 = md5($string_zmieniony);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://saldeo-test.brainshare.pl/api/xml/1.0/company/synchronize?command=$base64&req_id=$req_id&username=$username&req_sig=$md5",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

Related

Add an image to Drupal 7 using CURL

I'm migrating data from Zoho creator into Drupal 7. I wrote a PHP script to get all records and save them into nodes and that worked fine, but I need help to get the images and add them to the nodes as well. This is the code I have to get the image using CURL:
curl_setopt_array($curl, array(
CURLOPT_URL => $url.$id."/Image1/download",
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array("Authorization: Zoho-oauthtoken $token")
));
$image = curl_exec($curl);
What should I do after that?
You just need to save as file your data using file_save_data()
$image = curl_exec($curl);
// save as file in Drupal
$file = file_save_data($image);
var_dump($file->fid);
Then, to use as image you could use image_style_url() :
$imgfile = file_load($fid);
$url = image_style_url("thumb", $imgfile->uri);

How to get the response of Perl Post from Curl PHP

Good day! My client gave us an API endpoint where the response is XML when you pass in some commands, I would like to know if there's a way where we can run the endpoint in the PHP and get the response back, I have tried using PHP curl but the response shows 400 or bad request. I can access the endpoint with the commands/parameters in the URL browser so it works. But when I try it using curl, there is no response in it. Is this possible, or am I doing something wrong with my code?
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.winquote.net/cgi-bin/compete.pl?dc=-cv1.5 -ccca -qt0 -pccaXXXXXXXXX -rt0 -dob11061992 -gen1 -rR -fa500000 -pg0 -pi4 -lc1 -pm0 -rc0 -rop0 -langen -fmt -ceilp -faEXACT",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST"
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Have a look at White spaces in postFields in PHP Curl which may answer your question.
The only issue here is the query param are not proper and '400' response is saying you so. In browser you you look the spaces are converted to '%20'.The browser has done that work for you. You have to do the similar thing i.e. encode your query parameter because of the spaces before doing the post request.
From https://curl.haxx.se/docs/manpage.html#--data-urlencode
--data-urlencode
(HTTP) This posts data, similar to the other -d, --data options with
the exception that this performs URL-encoding.
To be CGI-compliant, the part should begin with a name followed
by a separator and a content specification. The part can be
passed to curl using one of the following syntaxes:

PHP file_get_contents booking.com Site

I try to get a booking.com page from a hotel to fetch the prices afterwards with regex. The problem is the following:
I call file_get_contents with parameter like checkin and checkout (file_get_contents("/hotel/at/myhotel.html?checkin=2017-10-12&checkout=2017-10-13")) dates so that the prices are shown to the visitor. If I watch the source code in the browser I see the entry:
b_this_url : '/hotel/at/myhotel.html?label=gen173nr-1FCAsoDkIcbmV1ZS1wb3N0LWhvbHpnYXUtaW0tbGVjaHRhbEgHYgVub3JlZmgOiAEBmAEHuAEHyAEM2AEB6AEB-AEDkgIBeagCAw;sid=58ccf750fc4acb908e20f0f28544c903;checkin=2017-10-12;checkout=2017-10-13;dist=0;sb_price_type=total;type=total&',
If I echo the string from file_get_contents the string looks like:
b_this_url : '/hotel/at/myhotel.html',
So all parameters that I passed to the url with file_get_contents are gone and therefore I couldn't find any prices with my regex on the page ...
Does anyone have a solution for this problem?
The webpage is not completely generated server-side, but it relies heavily on JavaScript after the HTML part loads. If you are looking for rendering the page as it looks in browser, I think you should use php curl instead of file_get_contents() for this kind of web scraping thing. I generated an automatic code for you from Postman (a google chrome extension / standalone desktop app) for your given url. The response contains the full url with params. See the image and I posted the code for you also.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.booking.com/hotel/at/hilton-innsbruck.de.html?checkin=2017-10-10%3Bcheckout%3D2017-10-11",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"postman-token: 581a75a7-6600-6ed6-75fd-5fb09c25d927"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

how to upload mp3 file and get its duration in laravel 4.2

I'd like to upload a MP3 file in Laravel and then get the duration of the MP3. Here's my code, but it's not working:
$music_file = Input::file('audio_path');
$file_music = date('Y-m-d-H-i-s')."-".$music_file->getClientOriginalName();
$path = public_path('assets/music/' . $file_music);
$path = $music_file->getRealPath();
for moving MP3
$music_file = Input::file('audio_path');
$file_music = date('Y-m-d-H-i-s')."-".$music_file->getClientOriginalName();
$path = public_path('assets/music/' . $file_music);
$music_file->move($path,$file_music);
After observing many doubts on this subject I will leave here a step by step with the solution I found for this problem.
For this we will use the getid3 package, a media file parser.
This solution was tested on Laravel 6.x, but it is compatible with several versions of Laravel.
1 - To install it on Laravel using the Composer, at the root of the project using the console type:
$ composer require james-heinrich/getid3
In this example we will create an endpoint to send an .mp3 file and return its duration.
2 - Create a Route to send the file:
Route::post('media', 'FilesController#media');
3 - Create a Controller to get the file and return the duration:
class FilesController extends Controller
{
public function media(Request $request)
{
$file = $request->file('upload');
if(!empty($request->file('upload'))) {
$getID3 = new \getID3;
$ThisFileInfo = $getID3->analyze($file);
$len= #$ThisFileInfo['playtime_string']; // playtime in minutes:seconds, formatted string
return $len;
}
}
4 - Sending the file:
Test using POSTMAN (good to check if everything in Laravel is OK):
Note: the upload field in POSTMAN is of type FILE.
Using Curl / PHP
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://localhost/api/media",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array('upload'=> new CURLFILE('/home/fellipe/Desktop/android-timeless.mp3')),
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded",
"Accept: application/json"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

Google Closure, Response Always Empty

I'm trying to get together a basic example of how to use Google Closure to minify JS. I can't seem to get this to work at all.
I'm trying to follow these examples:
https://developers.google.com/closure/compiler/docs/gettingstarted_api
http://closure-compiler.appspot.com/home
When working on API's and/or AJAX code, the first thing I try to is get the variables and values setup properly using just Advanced Rest Client Applications - a Chrome Extension. Whenever I send this data, though, I get an empty response (image below).
Trying to insert the same code into my PHP code, no matter what I send in the $postData variable, I get an empty (null) response.
PHP Code:
$postData =
http_build_query(
[
'output_info' => 'compiled_code',
'output_format' => 'text',
'compilation_level' => 'SIMPLE_OPTIMIZATIONS',
'js_code' => urlencode("function hello(name) { // Greets the user alert('Hello, ' + name); } hello('New user');")
]
);
$ret = $this->ci->curl->simple_post(
$url,
$postData,
$options
);
var_dump($ret);
die();
Response:
string ' ' (length=1)
I'm 99% confident that I'm missing something to use the Closure API like a key or something, but I have no idea how to proceed.
After many, many, many attempts, I found that if I used rawurlencode() instead of urlencode(), it works. Here's the final function.
// use google closure to get compiled JS
$encoded = rawurlencode($js);
$postData =
'output_info=compiled_code&' .
'output_format=text&' .
'compilation_level=WHITESPACE_ONLY&' .
'js_code=' . $encoded
;
$options = [];
$call = curl_init();
curl_setopt_array(
$call,
array(
CURLOPT_URL => 'http://closure-compiler.appspot.com/compile',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => 0,
CURLOPT_FOLLOWLOCATION => 0
)
);
$jscomp = curl_exec($call);
return $jscomp;

Categories