首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误“当试图将请求体解析为JSON时,无法读取它”

错误“当试图将请求体解析为JSON时,无法读取它”
EN

Stack Overflow用户
提问于 2013-12-17 19:54:01
回答 1查看 1K关注 0票数 0

我们的开发人员在API (Bright珍珠)上遇到了一些小问题,在尝试运行下面的脚本时,收到了一个“请求体无法读取,同时试图将其解析为JSON”的消息。有人对这个可能的问题有什么想法吗?在此之前,非常感谢您。

代码语言:javascript
复制
<?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


}



?>
EN

回答 1

Stack Overflow用户

发布于 2013-12-17 20:20:08

我不能完全确定,但我相信您的问题在于第二个curl请求。您正在传递一个标头-> Content-Type: application/json;charset=UTF-8 <-连同您的请求。但是,将数据编码为url字符串-> CURLOPT_POSTFIELDS,http_build_query($newShippingWarehouseDetails));

试试这个:

更改如下:

代码语言:javascript
复制
CURLOPT_POSTFIELDS,http_build_query($newShippingWarehouseDetails));

to此文:

代码语言:javascript
复制
CURLOPT_POSTFIELDS, json_encode($newShippingWarehouseDetails));

如果数据被正确地编码为JSON,我认为您应该没有问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20643431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档