Home > Blockchain >  I can write Php string in json but not working
I can write Php string in json but not working

Time:03-07

My code working. But if add get url and string not working. I want use php variable in json.

My proplem this not working :

$nam =  $nam = $_GET['tracknumber'];
"TrackingNumber": "echo json_encode($nam);"

It works when I write the normal cargo number in front of the Track Number. But it does not work when I write a PHP variable.

$nam = $_GET['tracknumber'];
$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://onlinetools.ups.com/ship/v1/shipments/labels',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS =>'{
        "LabelRecoveryRequest": {
            "LabelSpecification": {
                "HTTPUserAgent": "",
                "LabelImageFormat": {
                    "Code": "GIF"
                }

                },

                "LabelDelivery": {
                    "LabelLinkIndicator": "",
                    "ResendEMailIndicator": "",
                    "EMailMessage": {
                        "EMailAddress": ""
                    }
                    },


                    "TrackingNumber": "echo json_encode($nam);"

                }
            }',
            CURLOPT_HTTPHEADER => array(
                'Username: xxxxx',
                'Password: xxxxx',
                'AccessLicenseNumber: xxxxxxx',
                'Content-Type: application/json'
            ),
        ));

$response = curl_exec($curl);

curl_close($curl);

preg_match_all('@"GraphicImage":"(.*?)",@', $response, $veri);

echo "<img src=data:image/jpeg;base64,".$veri[1][0].">";

CodePudding user response:

You don't need to use echo :

"TrackingNumber": "echo json_encode($nam);"

Use :

"TrackingNumber": $nam

Or you can use json_encode :

$nam = $_GET['tracknumber'];
$curl = curl_init();

curl_setopt_array($curl, [
    CURLOPT_URL => 'https://onlinetools.ups.com/ship/v1/shipments/labels',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => json_encode([
        'LabelRecoveryRequest' => [
            'LabelSpecification' => [
                'HTTPUserAgent' => '',
                'LabelImageFormat' => [
                    'Code' => 'GIF',
                ],
            ],
            'LabelDelivery' => [
                'LabelLinkIndicator' => '',
                'ResendEMailIndicator' => '',
                'EMailMessage' => [
                    'EMailAddress' => '',
                ],
            ],
            'TrackingNumber' => $nam,
        ],
    ]),
    CURLOPT_HTTPHEADER => array(
        'Username: xxxxx',
        'Password: xxxxx',
        'AccessLicenseNumber: xxxxxxx',
        'Content-Type: application/json'
    ),
]);

$response = curl_exec($curl);

curl_close($curl);
preg_match_all('@"GraphicImage":"(.*?)",@', $response, $veri);
echo "<img src=data:image/jpeg;base64,".$veri[1][0].">";
  • Related