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 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低