使用cURL后的错误重定向URL

时间:2017-09-15 11:33:31

标签: php redirect curl

我有从邮递员复制的PHP代码,当从我的网站执行此代码时,我收到错误,我从cURL返回的重定向网址包含转义字符。如何告诉cURL不改变重定向字符串?

我得到的信息是: cURL错误:错误的网址{"重定向":" http:\ / \ / ...(< - 添加空格以使其可见,错误消息中确实存在,在此之后,url继续,每个斜杠都以反斜杠开始)

我的代码是:

        $curl = curl_init();
        curl_setopt_array($curl, array(
          CURLOPT_URL => "https:////portal.reeleezee.nl//api//v1//{removed adminid}//salesinvoices//36bed12c-4204-4e56-ba23-0da56ca9dccd",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 30,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "PUT",
          CURLOPT_POSTFIELDS => @'{"Date": "2017-09-21T00:00:00", "Header": "Subject: Sales invoice to H n L Company on purchase of flash drives", "Status": 1, "DocumentType": 10,   "Origin": 2,   "Type": 1, "DocumentLineList": [ { "id":"4c61e490-cbef-431a-be74-0fc2ab581e4f", "Sequence": 2, "Quantity": 2, "Price": 135, "Description": "Allium Gladiator His Excellence", "InvoiceLineType": 4, "DiscountAmount": null,  "DiscountPercentage": null,  "TaxRate": { "id": "1e44993a-15f6-419f-87e5-3e31ac3d9383" }    }  ],    "Entity":  { "id": "d7ae1602-4649-48dc-a75a-a9eafea7938f",    }}',
          CURLOPT_HTTPHEADER => array(
            "accept: application/json",
            "accept-language: en",
            "authorization: Basic {removed auth}",
            "cache-control: no-cache",
            "content-type: application/json",
            "postman-token: {removed}",
            "prefer: return=representation"
          ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
          echo "cURL Error BAH #:" . $err;
        } else {
          echo $response;
        }

2 个答案:

答案 0 :(得分:0)

错误消息不是来自cURL语句。如果我有一个回声'在我的函数中的某个地方,重定向网址将被转义......

不知道如何或为什么,但我的问题已经解决(echo语句仅用于存在错误)

答案 1 :(得分:0)

你传递给api的网址必须是urlencoded使用php urlencode()函数。它会解决你的问题;