使用Paypal RESTAPI和PHP cURL张贴发票(草稿)不断返回415不支持的媒体类型


Using Paypal REST api and PHP cURL to post invoice (draft) constantly returns 415 Unsupported Media Type

我使用贝宝的创建发票样本json从他们的网站

我得到了一个令牌,我在帖子中提交了这个令牌,因为错误不是401未经授权,而是415不受支持的媒体类型(现在是500内部服务器错误),这让我相信我很可能得到了正确的部分。。。所以我会提供一些代码,希望有人能发现我明显遗漏的东西:

createPaypalInvoice.php

function paypalCreateDraftRequest($token,$jsondraft){
    global $url;
    $sUrl = "/v1/invoicing/invoices";
    $accept = "Accept:application/json";
    $acceptlang = "Accept-Language:en_US";
    $contenttype = "Content-Type: application/json";
    $contentlen = "Content-Length: " . strlen($jsondraft);
            // $jsondraft is a string of json from paypal example
    $authorization = "Authorization: Bearer ".$token["access_token"];
    $headers = array($accept,$acceptlang,$contenttype,$contentlen,$authorization);
    $fields_string = $jsondraft;
    $ch = curl_init();
    $options = array (CURLOPT_RETURNTRANSFER => true, // return web page
        CURLOPT_FAILONERROR => true,
        CURLOPT_URL => $url.$sUrl,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => $fields_string,
        CURLOPT_HEADER => false,                      // don't return headers
        CURLOPT_SSL_VERIFYPEER => false,              // TODO: fix certificate provider list problem later
        CURLOPT_CONNECTTIMEOUT => 6,                  // timeout on connect
        CURLOPT_TIMEOUT => 10  );                      // timeout on response
    curl_setopt_array ( $ch, $options );
    try {
        $result=curl_exec( $ch );
        $log->LogDebug($result);
        //$header = curl_getinfo ( $ch );
        $err = curl_errno ( $ch );
        $errmsg = curl_error ( $ch );
        curl_close($ch);
        if ($errmsg) throw new Exception($errmsg);
        return $result;
     } catch (Exception $e) {  throw $e;  }
}

来自贝宝的发票示例:

{
    "merchant_info": {
    "email": "dennis@sample.com",
        "first_name": "Dennis",
        "last_name": "Doctor",
        "business_name": "Medical Professionals, LLC",
        "phone": {
        "country_code": "001",
            "national_number": "5032141716"
    },
    "address": {
        "line1": "1234 Main St.",
            "city": "Portland",
            "state": "OR",
            "postal_code": "97217",
            "country_code": "US"
    }
},
    "billing_info": [
    {
        "email": "example@example.com"
    }
],
    "items": [
    {
        "name": "Sutures",
        "quantity": 100,
        "unit_price": {
            "currency": "USD",
            "value": 5
        }
    }
],
    "note": "Medical Invoice 16 Jul, 2013 PST",
    "payment_term" :{
    "term_type": "NET_45"
},
    "shipping_info": {
    "first_name": "Sally",
        "last_name": "Patient",
        "business_name": "Not applicable",
        "address": {
        "line1": "1234 Broad St.",
            "city": "Portland",
            "state": "OR",
            "postal_code": "97216",
            "country_code": "US"
        }
    }
}

我发现了问题:

来自贝宝的发票文档:

注意:发票中指定的商家必须有PayPal账户信誉良好。

显然是样本账户"dennis@sample.com"不应该使用。当我使用我自己的沙盒帐户获取商家信息时,所有都神奇地工作了…

也许其他人可能有这个问题,现在已经公布了解决方案。也许我的示例代码也有帮助。。。

我在PayPal REST文档中找不到显示Accept/Accept语言使用的示例。

$accept = "Accept:application/json";
$acceptlang = "Accept-Language:en_US";

我会从你的标题中删除这些,看看这是否会改变你得到的错误。