3,5paste.cc

::

a javascript pastebin

  • New paste
  • View pastes

  • Login

  • About
  • PHP

    Paste: /b561131f/ReplyRaw

    400 Bad Request when using with paypal api

    function fetch($url, $method = "GET", $data = NULL, $content_type) {
        $context = [
            "method" => $method,
            "protocol_version" => "1.1"
        ];
        $headers = [
            "Content-Type: " . $content_type,
            "Connection: close"
        ];
        if($data) {
            $context["content"]
                = $content_type === "application/json"
                ? json_encode($data)
                : http_build_query($data);
            array_push($headers, "Content-Length: " . strlen($context["content"]));
        }
        for($i = 4; $i < func_num_args(); $i++)
            array_push($headers, func_get_arg($i));
        if($headers)
            $context["header"] = $headers;
        return file_get_contents($url, false, stream_context_create(["http" => $context]));
    }
    
    $res = fetch("https://api.sandbox.paypal.com/v1/payments/payment", "POST", $payment, "application/json", "Authorization: Bearer " . $token);
    
    //res is false
    
    //Warning: file_get_contents(https://api.sandbox.paypal.com/v1/payments/payment): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request