Home > Mobile >  Replacing characters in a returned Header Location in PHP
Replacing characters in a returned Header Location in PHP

Time:05-07

I'm trying to filter returned header location after POST using below PHP code. The returned header location is required for further processing of the payment status and saving the status in the db. The API provider seems not supportive since they don't reply on time or fail to reply at all.

    $output = curl_exec($curl);
    $lines = explode("\n",$output);
    $out = array();
    $headers = true;

    foreach ($lines as $l){
        $l = trim($l);
        if ($headers && !empty($l)){
           if (strpos($l,'location') !== false){
                $p = explode(' ',$l);
                $out['Headers']['location'] = trim($p[1]);
                $url = json_encode($out['Headers']['location']);
                echo json_encode($out['Headers']['location']);
            } 
        } 
    } 

The echo output is as below:- "https:\/\/sandbox.kopokopo.com\/api\/v1\/payments\/c122c1d2-8e07-48d3-8c9d-597829447fda" How do I make the output to be a valid url without "\" ? I'll really appreciate your valuable assistance.

CodePudding user response:

Your json_encode call is causing the problem, by escaping the / values.

Demo: https://3v4l.org/S3VWD

There's no need to encode a single string like this. JSON is mainly useful when you have a more complex set of information (e.g. multiple separate data items) that you want to output in a structured way.

echo $out['Headers']['location'];

is all you need in this case.

  •  Tags:  
  • php
  • Related