Home > front end >  Embedded variable in php curl request cannot be parsed (error 102)
Embedded variable in php curl request cannot be parsed (error 102)

Time:08-11

I am using php with yii framework to integrate with an external API. The goal is to submit a form that sends a request with 2 parameters to an internal endpoint (the action method described below), then have that action method forward the parameters along to the external API.

Since the parameters being sent are coming from a form, I need to be able to dynamically set them in the action method that queries the external API. I am doing this by setting the parameters from the front end request into variables, then embedding those variables in CURLOPT_POSTFIELDS.

The problem:

I receive the expected response if I hard code both parameter values into the action method, or if I embed only the $propertyId variable. When I embed the $customerName variable I receive the error response from the external API:

{"error":{"code":102,"message":"The format of your request body can not be parsed."}}}

I inserted a var_dump immediately following the $customerName variable assignment and confirmed it is indeed a string, which matches the expected data type based on the external API documentation.

string(9) "Test Name"

This is where I'm stuck, because hard coding a string gets the expected response, but embedding a variable containing a string with precisely the same value does not. I thought there could be an issue on the external API side, but seeing as the failure occurs only under such a specific condition I feel I'm just missing something when it comes to embedding variables in strings.

Full action method:

public function actionSearch()
    {
        $request = Yii::$app->request->post('params');
        $propertyId = $request['propertyId'];
        $customerName = $request['customerName'];
        var_dump($customerName);

        try {
            $curl = curl_init();

            curl_setopt_array($curl, array(
                CURLOPT_URL => 'https://some/endpoint',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'GET',
                CURLOPT_POSTFIELDS =>'{
                    "auth": {
                        "type" : "basic"
                    },
                    "requestId" : 15,
                    "method": {
                        "name": "searchCustomers",
                        "version":"r1",
                        "params": {
                            "propertyId" : '. $propertyId .',
                            "search" : '. $customerName .' // problematic parameter
                        }
                    }
                }',
                CURLOPT_HTTPHEADER => array(
                    'Content-Type: APPLICATION/JSON; CHARSET=UTF-8',
                    'Authorization: Basic',
                ),
            ));

            $response = curl_exec($curl);

            curl_close($curl);
            return json_decode($response, true);
        } catch(\Exception $e) {
            throw new LogicException('Error fetching customer data');
        }
    }

Alternative working CURLOPT_POSTFIELDS

CURLOPT_POSTFIELDS =>'{
    "auth": {
        "type" : "basic"
    },
    "requestId" : 15,
    "method": {
        "name": "searchCustomers",
        "version":"r1",
        "params": {
            "propertyId" : "12345",
            "search" : "Test Name"
        }
    }
}'

request payload from front end

{
  params: {
    propertyId: "12345", 
    customerName: "Test Name"
  }
}

CodePudding user response:

You've missed the wrap around the variable. Since you're creating a JSON, strings needs to be wrap with double qoute.

You can either wrap it on assignment or in JSON object.

$customerName = '"'. $request['customerName'] . '"';

Or much better;

$customerName = sprintf('"%s"', $request['customerName']);
  • Related