Autor Beitrag
Ovoxo
Hält's aus hier
Beiträge: 1



BeitragVerfasst: Mi 21.10.15 18:41 
Hallo zusammen,

Ich bin in der Ausbildung und kenne mich daher nicht sehr gut aus.
Mein Arbeitsgeber hat mir einen Auftrag gegeben, eine PHP-Datei in C# zu übersetzten.
Da wir eine kleine Firma sind und mein Chef weg ist, habe ich niemanden in der Firma wo mir helfen kann.
Die PHP-Datei ruft die cUrl Funktion auf um eine Rest-Api anzusprechen.

Nun muss ich diese cUrl Funktion ins C# übersetzten doch leider weiss ich nicht wie.

Ich habe bereits im Google gesucht und bin auf libCurl-Biding gestossen, jedoch war dies nicht das richtige.
Nach weiterer suche bin ich auf HttpWebRequest und HttpClient gestossen.

Jedoch weiss ich nicht wie die PHP-Datei übersetzten.
Daher wäre ich sehr froh, wenn Ihr mir tipps geben könntet wie ich damit anfangen muss, welche Funktionen ich brauche und etc.

Nur um klar zustellen, ich will nicht das mir jemand den Code übersetzt. Ich will die aufgabe selber Lösen und brauche Hilfe beim Einstieg ins Thema.

Danke

Hier der PHP-Code:
ausblenden volle Höhe PHP-Quelltext
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:
    const METHOD_GET = 'GET';
    const METHOD_POST = 'POST';
    const METHOD_PUT = 'PUT';
    const METHOD_DELETE = 'DELETE';
    
    const CURL_TIMEOUT_IN_SECS = 15;

    public static $successFullHttpCodes = array(200201204);

    private function makeCurlCall($url$postParams = array(), $method = self::METHOD_GET, $headers = array()) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_VERBOSE, 0);
        curl_setopt($curl, CURLOPT_TIMEOUT, self::CURL_TIMEOUT_IN_SECS);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);

        if ($headers) {
            $usableHeaders = array();
            foreach ($headers as $name => $value) {
                $usableHeaders[] = sprintf('%s: %s'$name$value);
            }

            curl_setopt($curl, CURLOPT_HTTPHEADER, $usableHeaders);
        }

        if ($postParams) {
            curl_setopt($curl, CURLOPT_POSTFIELDS, $postParams);
        }

        $data = curl_exec($curl);
        $this->checkForError($curl$data);

        return $data;
    }

    private function checkForError($curl$data) {
        $curlInfo = curl_getinfo($curl);
        if (isset($curlInfo['http_code']) && !in_array($curlInfo['http_code'], self::$successFullHttpCodes)) {
            throw new Exception($curlInfo['http_code'] ? $data : 'could not get a response from the service'$curlInfo['http_code'] ? $curlInfo['http_code'] : 500);
        }
    }


Moderiert von user profile iconTh69: PHP-Tags hinzugefügt