Curl::post_data()   F
last analyzed

Complexity

Conditions 14
Paths 771

Size

Total Lines 44
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 36
c 1
b 0
f 0
nc 771
nop 1
dl 0
loc 44
rs 2.418

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace App\Babel\Submit;
3
4
use App\Models\Submission\SubmissionModel;
5
use Exception;
6
7
interface CurlInterface {
8
    function login($all_data);
9
    function grab_page($all_data);
10
    function post_data($all_data);
11
}
12
class Curl
13
{
14
    public function __construct()
15
    {
16
        //
17
    }
18
19
    public function login($all_data)
20
    {
21
        if (isset($all_data["url"]))    $url=$all_data["url"]; else throw new Exception("url is not exist in all_data");
22
        if (isset($all_data["data"]))   $data=$all_data["data"]; else throw new Exception("data is not exist in all_data");
23
        if (isset($all_data["oj"]))     $oj=$all_data["oj"]; else throw new Exception("oj is not exist in all_data");
24
        if (isset($all_data["ret"]))    $ret=$all_data["ret"]; else $ret='false';
25
        if (isset($all_data["handle"])) $handle=$all_data["handle"]; else $handle="default";
26
27
        $datapost=curl_init();
28
        $headers=array("Expect:");
29
        $handle=urlencode($handle);
30
31
        curl_setopt($datapost, CURLOPT_CAINFO, babel_path("Cookies/cacert.pem"));
32
        curl_setopt($datapost, CURLOPT_URL, $url);
33
        curl_setopt($datapost, CURLOPT_HEADER, true); //
34
        curl_setopt($datapost, CURLOPT_HTTPHEADER, $headers); //
35
        curl_setopt($datapost, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36");
36
        curl_setopt($datapost, CURLOPT_POST, true);
37
38
        curl_setopt($datapost, CURLOPT_RETURNTRANSFER, $ret);
39
        curl_setopt($datapost, CURLOPT_SSL_VERIFYPEER, false);
40
        curl_setopt($datapost, CURLOPT_FOLLOWLOCATION, true);
41
42
        curl_setopt($datapost, CURLOPT_POSTFIELDS, $data);
43
        curl_setopt($datapost, CURLOPT_COOKIEFILE, babel_path("Cookies/{$oj}_{$handle}.cookie"));
44
        curl_setopt($datapost, CURLOPT_COOKIEJAR, babel_path("Cookies/{$oj}_{$handle}.cookie"));
45
        ob_start();
46
        $response=curl_exec($datapost);
47
        if (curl_errno($datapost)) {
48
            throw new Exception(curl_error($datapost), 10000);
49
        }
50
        ob_end_clean();
51
        curl_close($datapost);
52
        unset($datapost);
53
        return $response;
54
    }
55
56
    public function grab_page($all_data)
57
    {
58
        if (isset($all_data["site"]))    $site=$all_data["site"]; else throw new Exception("site is not exist in all_data");
59
        if (isset($all_data["oj"]))      $oj=$all_data["oj"]; else throw new Exception("oj is not exist in all_data");
60
        if (isset($all_data["headers"])) $headers=$all_data["headers"]; else $headers=[];
61
        if (isset($all_data["handle"]))  $handle=$all_data["handle"]; else $handle="default";
62
        if (isset($all_data["follow"]))  $follow=$all_data["follow"]; else $follow=false;
63
        if (isset($all_data["vcid"]))  $vcid=$all_data["vcid"]."_"; else $vcid="";
64
65
        $handle=urlencode($handle);
66
67
        $ch=curl_init();
68
        curl_setopt($ch, CURLOPT_CAINFO, babel_path("Cookies/cacert.pem"));
69
        // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
70
        // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
71
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
72
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
73
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $follow);
74
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36");
75
        curl_setopt($ch, CURLOPT_COOKIEFILE, babel_path("Cookies/{$oj}_{$vcid}{$handle}.cookie"));
76
        curl_setopt($ch, CURLOPT_COOKIEJAR, babel_path("Cookies/{$oj}_{$vcid}{$handle}.cookie"));
77
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
78
        curl_setopt($ch, CURLOPT_URL, $site);
79
        ob_start();
80
        $response=curl_exec($ch);
81
        if (curl_errno($ch)) {
82
            throw new Exception(curl_error($ch), 10000);
83
        }
84
        ob_end_clean();
85
        curl_close($ch);
86
        return $response;
87
    }
88
89
    public function post_data($all_data)
90
    {
91
        if (isset($all_data["site"]))         $site=$all_data["site"]; else throw new Exception("site is not exist in all_data");
92
        if (isset($all_data["data"]))         $data=$all_data["data"]; else throw new Exception("data is not exist in all_data");
93
        if (isset($all_data["oj"]))           $oj=$all_data["oj"]; else throw new Exception("oj is not exist in all_data");
94
        if (isset($all_data["ret"]))          $ret=$all_data["ret"]; else $ret=false;
95
        if (isset($all_data["follow"]))       $follow=$all_data["follow"]; else $follow=true;
96
        if (isset($all_data["returnHeader"])) $returnHeader=$all_data["returnHeader"]; else $returnHeader=true;
97
        if (isset($all_data["postJson"]))     $postJson=$all_data["postJson"]; else $postJson=false;
98
        if (isset($all_data["extraHeaders"])) $extraHeaders=$all_data["extraHeaders"]; else $extraHeaders=[];
99
        if (isset($all_data["handle"]))       $handle=$all_data["handle"]; else $handle="default";
100
        if (isset($all_data["vcid"]))  $vcid=$all_data["vcid"]."_"; else $vcid="";
101
102
        $handle=urlencode($handle);
103
104
        $datapost=curl_init();
105
        $headers=array("Expect:");
106
        if ($postJson) {
107
            $data=$data ? json_encode($data) : '{}';
108
            array_push($headers, 'Content-Type: application/json', 'Content-Length: '.strlen($data));
109
        }
110
        curl_setopt($datapost, CURLOPT_CAINFO, babel_path("Cookies/cacert.pem"));
111
        curl_setopt($datapost, CURLOPT_URL, $site);
112
        curl_setopt($datapost, CURLOPT_HEADER, $returnHeader);
113
        curl_setopt($datapost, CURLOPT_HTTPHEADER, array_merge($headers, $extraHeaders));
114
        curl_setopt($datapost, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36");
115
        curl_setopt($datapost, CURLOPT_POST, true);
116
117
        curl_setopt($datapost, CURLOPT_RETURNTRANSFER, $ret);
118
        curl_setopt($datapost, CURLOPT_SSL_VERIFYPEER, false);
119
        curl_setopt($datapost, CURLOPT_FOLLOWLOCATION, $follow);
120
121
        curl_setopt($datapost, CURLOPT_POSTFIELDS, $data);
122
        curl_setopt($datapost, CURLOPT_COOKIEFILE, babel_path("Cookies/{$oj}_{$vcid}{$handle}.cookie"));
123
        curl_setopt($datapost, CURLOPT_COOKIEJAR, babel_path("Cookies/{$oj}_{$vcid}{$handle}.cookie"));
124
        ob_start();
125
        $response=curl_exec($datapost);
126
        if (curl_errno($datapost)) {
127
            throw new Exception(curl_error($datapost), 10000);
128
        }
129
        ob_end_clean();
130
        curl_close($datapost);
131
        unset($datapost);
132
        return $response;
133
    }
134
}
135