chengkun
2025-09-12 26c5c0296e7c094f9a7ae4a4bb3c975796992eaf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/*
 *  @author MST1122
 *  @email: sikandartariq25@gmail.com
 */
 
namespace DoubleBreak\Spapi\Helper;
 
 
use DoubleBreak\Spapi\ASECryptoStream;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
 
class Feeder
{
    /**
     * @param $payload : Response from createFeedDocument Function. e.g.: response['payload']
     * @param $contentType : Content type used during createFeedDocument function call.
     * @param $feedContentFilePath : Path to file that contain data to be uploaded.
     * @return string
     * @throws \Exception
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function uploadFeedDocument($payload, $contentType, $feedContentFilePath)
    {
        $key = null;
        $initializationVector = null;
        $feedUploadUrl = $payload['url'];
 
        // check if encryption in required
        if (isset($payload['encryptionDetails'])) {
            $key = $payload['encryptionDetails']['key'];
            $initializationVector = $payload['encryptionDetails']['initializationVector'];
 
            // base64 decode before using in encryption
            $initializationVector = base64_decode($initializationVector, true);
            $key = base64_decode($key, true);
        }
 
        // get file to upload
        $fileResourceType = gettype($feedContentFilePath);
 
        // resource or string ? make it to a string
        if ($fileResourceType == 'resource') {
            $file_content = stream_get_contents($feedContentFilePath);
        } else {
            $file_content = file_get_contents($feedContentFilePath);
        }
 
        // utf8 !
        $file_content = utf8_encode($file_content);
 
        if (!is_null($key)) {
            // encrypt string and get value as base64 encoded string
            $file_content = ASECryptoStream::encrypt($file_content, $key, $initializationVector);
        }
 
        // my http client
        $client = new Client(['exceptions' => false]);
 
        $request = new Request(
        // PUT!
            'PUT',
            // predefined url
            $feedUploadUrl,
            // content type equal to content type from response createFeedDocument-operation
            array('Content-Type' => $contentType),
            // resource File
            $file_content
        );
 
        $response = $client->send($request);
        $HTTPStatusCode = $response->getStatusCode();
 
        if ($HTTPStatusCode == 200) {
            return 'Done';
        } else {
            return $response->getBody()->getContents();
        }
    }
 
    /**
     * @param $payload : Response from getFeedDocument Function. e.g.: response['payload']
     * @return array : Feed Processing Report.
     */
    public function downloadFeedProcessingReport($payload)
    {
        $key = null;
        $initializationVector = null;
        $feedUploadUrl = $payload['url'];
 
        // check if decryption in required
        if (isset($payload['encryptionDetails'])) {
            $key = $payload['encryptionDetails']['key'];
            $initializationVector = $payload['encryptionDetails']['initializationVector'];
 
            // base64 decode before using in encryption
            $initializationVector = base64_decode($initializationVector, true);
            $key = base64_decode($key, true);
        }
 
        $feedDownloadUrl = $payload['url'];
 
        if (!is_null($key)) {
            $feed_processing_report_content = ASECryptoStream::decrypt(file_get_contents($feedDownloadUrl), $key, $initializationVector);
        } else {
            $feed_processing_report_content = file_get_contents($feedDownloadUrl);
        }
 
        if(isset($payload['compressionAlgorithm']) && $payload['compressionAlgorithm']=='GZIP') {
            $feed_processing_report_content = gzdecode($feed_processing_report_content);
        }
 
        // check if report content is json encoded or not
        if ($this->isJson($feed_processing_report_content) == true) {
            $json = $feed_processing_report_content;
        } else {
            $feed_processing_report_content = preg_replace('/\s+/S', " ", $feed_processing_report_content);
            $xml = simplexml_load_string($feed_processing_report_content);
            $json = json_encode($xml);
        }
 
        return json_decode($json, TRUE);
    }
 
    public function isJson($string) {
        json_decode($string);
        return json_last_error() === JSON_ERROR_NONE;
    }
}