dongyou2635 2014-09-01 13:18
浏览 24
已采纳

get_file_contents中的多个标头不起作用

I am trying to pass multiple headers through to a URL (API server) using file_get_contents. The code I have below echos out a HTTP 500 error:

$userdetails = http_build_query(
array(
    'userdetails' => '

                        {

                "jobId": null, "collectionOnDelivery": false, "invoice": null,

                "collectionDate": "2014-08-27T09:11:00", "consolidate": false,

                "consignments": [{ "collectionDetails":{

                "contactDetails":{ "contactName":"Mr David Smith" "telephone":"0121 500 2500"

                },

                "address":{

                "organisation":"GeoPost UK Ltd", "property":"",

                "street":"Roebuck Lane", "locality":"Smethwick", "town":"Birmingham", "county":"West Midlands", "postcode":"B66 1BY", "countryCode":"GB",

                }

                },

                "deliveryDetails":{

                "contactDetails":{ "contactName":"Mr David Smith"

                "telephone":"0121 500 2500"

                },

                "notificationDetails":{ "mobile":"07921 123456" "email":"david.smith@acme.com"

                },

                "address":{ "organisation":"ACME Ltd",

                "property":"Miles Industrial Estate", "street":"42 Bridge Road", "locality":"",

                "town":"Birmingham", "county":"West Midlands", "postcode":"B1 1AA", "countryCode":"GB",

                }

                },

                "networkCode":"1^12",

                "numberOfParcels":1,

                "totalWeight":5, "shippingRef1":"Catalogue Batch 1", "shippingRef2":"Invoice 231", "shippingRef3":"", "customsValue":0,

                "deliveryInstructions":"Please deliver to industrial gate A", "parcelDescription":"",

                "liabilityValue":0,

                "liability":false,

                "parcel":[]

                }]

                }



    '

    )
 );




$content = array('http' =>
    array(
         'method'  => 'POST',
         'header'  => "GEOSESSION: ".$geosession,
          "Accept: application/json",
           "Content-Type: application/json",
            'content' => $userdetails

     )
  );

   $context2  = stream_context_create($content);

   $result_userdetails = file_get_contents('https://api.interlinkexpress.com/shipping/shipment', false, $context2);


 echo $result_userdetails;

The thing is I have tried/tested the headers on POSTMAN and I am getting a response back however when I pass it through the code above I get the error. I am unsure why this is and was wondering if I am actually passing all three headers across or not? Could someone please clarify whether this is the case or if there is an error?

UPDATE:

I have tried changing the userdetails to the below as someone pointed out it was already in JSON format however the API does not seem to be liking the 'content' => $userdetails now?

$userdetails = $userdetails = http_build_query(
array(
    'userdetails' => '

                   {
"job_id": null,
"collectionOnDelivery": false,
"invoice": null,
"collectionDate": "",
"consolidate": false,
"consignment": [{
    "consignmentNumber": null,
    "consignmentRef": null,
    "parcels": [],
    "collectionDetails": {
        "contactDetails": {
            "contactName": "My Contact",
            "telephone": "0121 500 2500"
        },
        "address": {
            "organisation": "GeoPostUK Ltd",
            "countryCode": "GB",
            "postcode": "B66 1BY",
            "street": "Roebuck Lane",
            "locality": "Smethwick",
            "town": "Birmingham",
            "county": "West Midlands"
        }
    },
    "deliveryDetails": {
        "contactDetails": {
            "contactName": "My Contact",
            "telephone": "0121 500 2500"
        },
        "address": {
            "organisation": "GeoPostUK Ltd",
            "countryCode": "GB",
            "postcode": "B66 1BY",
            "street": "Roebuck Lane",
            "locality": "Smethwick",
            "town": "Birmingham",
            "county": "West Midlands"
        },
        "notificationDetails": {
            "email": "my.email@geopostuk.com",
            "mobile": "07921000001"
        }
    },
    "networkCode": "2^12",
    "numberOfParcels": 1,
    "totalWeight": 5,
    "shippingRef1": "My Ref 1",
    "shippingRef2": "My Ref 2",
    "shippingRef3": "My Ref 3",
    "customsValue": null,
    "deliveryInstructions": "Please deliver with neighbour",
    "parcelDescription": "",
    "liabilityValue": null,
    "liability": false
}]

}

  • 写回答

1条回答 默认 最新

  • doushaiyu5065 2014-09-01 13:30
    关注

    Multiple headers should be connected using CRLF

    $content = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => "GEOSESSION: " . $geosession . "
    " .
                         "Accept: application/json
    " .
                         "Content-Type: application/json",
            'content' => $userdetails
    
        )
    );
    

    To encode the data properly, first create a PHP containing the data, and then use json_encode to format it:

    $data = array("jobId" => null, "collectionOnDelivery" => false, "invoice" => null,
    
                  "collectionDate" => "2014-08-27T09:11:00", "consolidate" => false,
    
                  "consignments" => array(array( "collectionDetails" =>
                                                 array("contactDetails" => array( "contactName" => "Mr David Smith" "telephone" => "0121 500 2500"),
                                                       "address" => array("organisation" => "GeoPost UK Ltd", "property" => "",
                                                                          "street" => "Roebuck Lane", "locality" => "Smethwick", "town" => "Birmingham",
                                                                          "county" => "West Midlands", "postcode" => "B66 1BY", "countryCode" => "GB",)
                                                       ),
                                                 "deliveryDetails" => array("contactDetails" => array( "contactName" => "Mr David Smith"
                                                                                                       "telephone" => "0121 500 2500"
                                                                                                       ),
                                                                            "notificationDetails" => array( "mobile" => "07921 123456" "email" => "david.smith@acme.com"),
                                                                            "address" => array( "organisation" => "ACME Ltd",
                                                                                                "property" => "Miles Industrial Estate", "street" => "42 Bridge Road", "locality" => "",
                                                                                                "town" => "Birmingham", "county" => "West Midlands", "postcode" => "B1 1AA", "countryCode" => "GB",)
                                                                            ),
                                                 "networkCode" => "1^12",
                                                 "numberOfParcels" => 1,
                                                 "totalWeight" => 5, "shippingRef1" => "Catalogue Batch 1", "shippingRef2" => "Invoice 231", "shippingRef3" => "", "customsValue" => 0,
                                                 "deliveryInstructions" => "Please deliver to industrial gate A", "parcelDescription" => "",
                                                 "liabilityValue" => 0,
                                                 "liability" => false,
                                                 "parcel" => array()
                                                 ))
                  );
    $userdetails = json_encode($data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 verilog modelsim仿真
  • ¥15 Power BI 里面 帕累托图突出显示前20
  • ¥50 用预估矫正法,分数阶微分方程组传染病的最优控制代码怎么写
  • ¥15 画个数据流程图,手画也行
  • ¥60 AS自带模拟器AVD Root 和 Xposed安装
  • ¥30 哪位搞Android的编程师可以帮我远程一下,悬赏30元
  • ¥15 solidity部署上合约,可以mint,也继承接口了,在区块链也不显示代币名字
  • ¥15 讨论 博士论文交互项怎么讨论?
  • ¥50 在集成BiLSTM和GCN模型时遇到了问题
  • ¥33 集成BiLSTM模型和GCN模型时,前模型的输出不匹配后模型的输入