Passed
Branch feature/babel (9a8402)
by John
03:52
created

Curl::post_data()   F

Complexity

Conditions 13
Paths 387

Size

Total Lines 40
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 33
nc 387
nop 1
dl 0
loc 40
rs 3.4458
c 0
b 0
f 0

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\SubmissionModel;
5
6
class Curl
7
{
8
    public function __construct()
9
    {
10
        //
11
    }
12
13
    protected function login($all_data)
14
    {
15
        if(isset($all_data["url"]))    $url = $all_data["url"];       else throw new Exception("url is not exist in all_data");
0 ignored issues
show
Bug introduced by
The type App\Babel\Submit\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
16
        if(isset($all_data["data"]))   $data = $all_data["data"];     else throw new Exception("data is not exist in all_data");
17
        if(isset($all_data["oj"]))     $oj = $all_data["oj"];         else throw new Exception("oj is not exist in all_data");
18
        if(isset($all_data["ret"]))    $ret = $all_data["ret"];       else $ret = 'false';
19
        if(isset($all_data["handle"])) $handle = $all_data["handle"]; else $handle = "default";
20
21
        $datapost=curl_init();
22
        $headers=array("Expect:");
23
24
        curl_setopt($datapost, CURLOPT_CAINFO, dirname(__FILE__)."/../Cookies/cacert.pem");
0 ignored issues
show
Bug introduced by
It seems like $datapost can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

24
        curl_setopt(/** @scrutinizer ignore-type */ $datapost, CURLOPT_CAINFO, dirname(__FILE__)."/../Cookies/cacert.pem");
Loading history...
25
        curl_setopt($datapost, CURLOPT_URL, $url);
26
        curl_setopt($datapost, CURLOPT_HEADER, true); //
27
        curl_setopt($datapost, CURLOPT_HTTPHEADER, $headers); //
28
        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");
29
        curl_setopt($datapost, CURLOPT_POST, true);
30
31
        curl_setopt($datapost, CURLOPT_RETURNTRANSFER, $ret);
32
        curl_setopt($datapost, CURLOPT_FOLLOWLOCATION, true);
33
34
        curl_setopt($datapost, CURLOPT_POSTFIELDS, $data);
35
        curl_setopt($datapost, CURLOPT_COOKIEFILE, dirname(__FILE__)."/../Cookies/{$oj}_{$handle}.cookie");
36
        curl_setopt($datapost, CURLOPT_COOKIEJAR, dirname(__FILE__)."/../Cookies/{$oj}_{$handle}.cookie");
37
        ob_start();
38
        $response=curl_exec($datapost);
0 ignored issues
show
Bug introduced by
It seems like $datapost can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
        $response=curl_exec(/** @scrutinizer ignore-type */ $datapost);
Loading history...
39
        if (curl_errno($datapost)) {
0 ignored issues
show
Bug introduced by
It seems like $datapost can also be of type false; however, parameter $ch of curl_errno() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        if (curl_errno(/** @scrutinizer ignore-type */ $datapost)) {
Loading history...
40
            die(curl_error($datapost));
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
Bug introduced by
It seems like $datapost can also be of type false; however, parameter $ch of curl_error() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
            die(curl_error(/** @scrutinizer ignore-type */ $datapost));
Loading history...
41
        }
42
        ob_end_clean();
43
        curl_close($datapost);
0 ignored issues
show
Bug introduced by
It seems like $datapost can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
        curl_close(/** @scrutinizer ignore-type */ $datapost);
Loading history...
44
        unset($datapost);
45
        return $response;
46
    }
47
48
    protected function grab_page($all_data)
49
    {
50
        if(isset($all_data["site"]))    $site = $all_data["site"];          else throw new Exception("site is not exist in all_data");
51
        if(isset($all_data["oj"]))      $oj = $all_data["oj"];              else throw new Exception("oj is not exist in all_data");
52
        if(isset($all_data["headers"])) $headers = $all_data["headers"];    else $headers = [];
53
        if(isset($all_data["handle"]))  $handle = $all_data["handle"];      else $handle = "default";
54
55
        $ch=curl_init();
56
        curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__)."/../Cookies/cacert.pem");
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_CAINFO, dirname(__FILE__)."/../Cookies/cacert.pem");
Loading history...
57
        // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
58
        // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
59
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
60
        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");
61
        curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__)."/../Cookies/{$oj}_{$handle}.cookie");
62
        curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__)."/../Cookies/{$oj}_{$handle}.cookie");
63
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
64
        curl_setopt($ch, CURLOPT_URL, $site);
65
        ob_start();
66
        $response=curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
        $response=curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
67
        if (curl_errno($ch)) {
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_errno() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
        if (curl_errno(/** @scrutinizer ignore-type */ $ch)) {
Loading history...
68
            die(curl_error($ch));
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_error() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
            die(curl_error(/** @scrutinizer ignore-type */ $ch));
Loading history...
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
69
        }
70
        ob_end_clean();
71
        curl_close($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
72
        return $response;
73
    }
74
75
    protected function post_data($all_data)
76
    {
77
        if(isset($all_data["site"]))         $site = $all_data["site"];                 else throw new Exception("site is not exist in all_data");
78
        if(isset($all_data["data"]))         $data = $all_data["data"];                 else throw new Exception("data is not exist in all_data");
79
        if(isset($all_data["oj"]))           $oj = $all_data["oj"];                     else throw new Exception("oj is not exist in all_data");
80
        if(isset($all_data["ret"]))          $ret = $all_data["ret"];                   else $ret = false;
81
        if(isset($all_data["follow"]))       $follow = $all_data["follow"];             else $follow = true;
82
        if(isset($all_data["returnHeader"])) $returnHeader = $all_data["returnHeader"]; else $returnHeader = true;
83
        if(isset($all_data["postJson"]))     $postJson = $all_data["postJson"];         else $postJson = false;
84
        if(isset($all_data["extraHeaders"])) $extraHeaders = $all_data["extraHeaders"]; else $extraHeaders = [];
85
        if(isset($all_data["handle"]))       $handle = $all_data["handle"];             else $handle = "default";
86
87
        $datapost=curl_init();
88
        $headers=array("Expect:");
89
        if ($postJson) {
90
            $data=$data ? json_encode($data) : '{}';
91
            array_push($headers, 'Content-Type: application/json', 'Content-Length: '.strlen($data));
92
        }
93
        curl_setopt($datapost, CURLOPT_CAINFO, dirname(__FILE__)."/../Cookies/cacert.pem");
0 ignored issues
show
Bug introduced by
It seems like $datapost can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
        curl_setopt(/** @scrutinizer ignore-type */ $datapost, CURLOPT_CAINFO, dirname(__FILE__)."/../Cookies/cacert.pem");
Loading history...
94
        curl_setopt($datapost, CURLOPT_URL, $site);
95
        curl_setopt($datapost, CURLOPT_HEADER, $returnHeader);
96
        curl_setopt($datapost, CURLOPT_HTTPHEADER, array_merge($headers, $extraHeaders));
97
        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");
98
        curl_setopt($datapost, CURLOPT_POST, true);
99
100
        curl_setopt($datapost, CURLOPT_RETURNTRANSFER, $ret);
101
        curl_setopt($datapost, CURLOPT_FOLLOWLOCATION, $follow);
102
103
        curl_setopt($datapost, CURLOPT_POSTFIELDS, $data);
104
        curl_setopt($datapost, CURLOPT_COOKIEFILE, dirname(__FILE__)."/../Cookies/{$oj}_{$handle}.cookie");
105
        curl_setopt($datapost, CURLOPT_COOKIEJAR, dirname(__FILE__)."/../Cookies/{$oj}_{$handle}.cookie");
106
        ob_start();
107
        $response=curl_exec($datapost);
0 ignored issues
show
Bug introduced by
It seems like $datapost can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
        $response=curl_exec(/** @scrutinizer ignore-type */ $datapost);
Loading history...
108
        if (curl_errno($datapost)) {
0 ignored issues
show
Bug introduced by
It seems like $datapost can also be of type false; however, parameter $ch of curl_errno() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
        if (curl_errno(/** @scrutinizer ignore-type */ $datapost)) {
Loading history...
109
            die(curl_error($datapost));
0 ignored issues
show
Bug introduced by
It seems like $datapost can also be of type false; however, parameter $ch of curl_error() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

109
            die(curl_error(/** @scrutinizer ignore-type */ $datapost));
Loading history...
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
110
        }
111
        ob_end_clean();
112
        curl_close($datapost);
0 ignored issues
show
Bug introduced by
It seems like $datapost can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
        curl_close(/** @scrutinizer ignore-type */ $datapost);
Loading history...
113
        unset($datapost);
114
        return $response;
115
    }
116
}
117