dtlygweb2017 2013-12-17 19:54
浏览 84

错误“尝试将其解析为JSON时无法读取请求正文”

Our developer is having a little problem with an API (Brightpearl) and receiving a "The request body could not be read while attempting to parse it as JSON" message when trying to run the below script. Does anyone have any ideas on the possible issue? Many thanks in advance.

<?php
define ("DB_HOST","localhost");
define ("DB_USER","busi6292_REMOVED");
define ("DB_PASSWORD","REMOVED");
define ("DB_DATABASE","busi6292_REMOVED");

$link = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
or die ('I cannot connect to the database because: ' . @mysql_error());
@mysql_select_db (DB_DATABASE);
mysql_set_charset('utf8',$link);

// Brightperl Data Access Start

    $authenticationDetails = array(
    'apiAccountCredentials' => array(
        'emailAddress' => 'REMOVED',
        'password'     => 'REMOVED',
    ),
    );
    $encodedAuthenticationDetails = json_encode($authenticationDetails);
    $authenticationUrl = 'https://ws-eu1.brightpearl.com/REMOVED/authorise';

    //$cresponse_token=getdata($url,$authenticationDetails);

    $ch = curl_init(); 

    $headers = array('Content-Type: application/json');
    curl_setopt($ch, CURLOPT_URL, $authenticationUrl);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($authenticationDetails));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    if (false === $response) {
        echo 'Request unsuccessful' . PHP_EOL;
        curl_close($ch);
        exit(1);
    }
    $responseCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $responseBody = json_decode($response);
    curl_close($ch);

    $authorisationToken = $responseBody->response;

// End



$sql = "select * from mail where InvoiceDate ='".date('Y-m-d')."'";
$query = mysql_query($sql);

while($sql_array = mysql_fetch_array($query))
{

    //print_r($sql_array);

    //echo $authorisationToken; exit;


    // Brigtperl API Access for the Orders has been shipped Start

    $ShippingGoodsoutURL = 'https://ws-eu1.brightpearl.com/2.0.0/REMOVED/warehouse-service/goods-note/goods-out/'.$sql_array['OrderNo'];

    $headers = array(
    "brightpearl-auth: {$authorisationToken}",
    'Content-Type: application/json;charset=UTF-8',
    'X-HTTP-Method-Override: PUT',
    );
    $newShippingWarehouseDetails = array(
    "priority" => false,
    "shipping" => 
    array(
            "reference" =>'"'.$sql_array['InvoiceNumber'].'"',
            ),
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $ShippingGoodsoutURL);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_PUT, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    //curl_setopt($ch, CURLOPT_POST, true);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($newShippingWarehouseDetails));


    curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($newShippingWarehouseDetails));

    $GoodsoutreShippingResponse = curl_exec($ch);
    curl_close($ch);

    print_r($GoodsoutreShippingResponse);

    //exit;

    // End


}



?>
  • 写回答

1条回答 默认 最新

  • douyan6871 2013-12-17 20:20
    关注

    I can't be entirely sure, but I believe your problem lies with the second curl request. You're passing a header -> Content-Type: application/json;charset=UTF-8 <- with your request. However, your encoding your data as a url string -> CURLOPT_POSTFIELDS,http_build_query($newShippingWarehouseDetails));.

    Try this:

    Change this:

    CURLOPT_POSTFIELDS,http_build_query($newShippingWarehouseDetails));
    

    to this:

    CURLOPT_POSTFIELDS, json_encode($newShippingWarehouseDetails));
    

    If the data is properly encoded as JSON, I don't think you should have a problem.

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)