PHP中带有参数的多部分发布json请求

时间:2018-12-15 22:09:07

标签: php curl

我有一个服务器,该服务器接收带有参数 trip 的json数据多部分请求。我试图向该服务器发送请求,但似乎找不到解决方法。我已经检查过了,但没有一个答案能解决我的问题。

这是到目前为止所做的

function postToServerWithAuthAsMultipart($service_url, $curl_post_data, $ff_token) {
        $curl = curl_init($service_url);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, true);


        $payload = json_encode(array('trip' => $curl_post_data));//Want to parse trip as a parameter

        curl_setopt($curl, CURLOPT_HTTPHEADER,  array('Content-Type: multipart/form-data', 'Accept: application/json', 'Content-Length: ' . strlen($payload), 'Authorization: ' . $ff_token));
        curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($curl, CURLOPT_VERBOSE, 1);

        $curl_response = curl_exec($curl);

        curl_close($curl);

        return ($curl_response);

    } 

请感谢您的帮助,以下是我打算(迅速)实现的目标。谢谢

let headers: HTTPHeaders = [
            "Authorization": authToken,
            "Accept": "application/json"
        ]

        Alamofire.upload(multipartFormData:{ multipartFormData in
            multipartFormData.append(data, withName: "trip")},
            usingThreshold:UInt64.init(),
            to: OFFER_RIDE_URL,
            method:.post,
            headers: headers,
            encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in

                    debugPrint(response)
                    guard let json = response.result.value as? [String : AnyObject] else {
                        print("Failed to get expected response from webserver.")
                        return
                    }


            case .failure(let encodingError):
                //print(encodingError)
            }
        })

0 个答案:

没有答案