1

So far I manage to get HTTP response header from this

$ch = curl_init();<br/>
$url="http://localhost/PHP_Projects/Test/response.php";

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch);

$headers = get_headers_from_curl_response($response);

foreach($headers as $x => $x_value){<br/>
    print $x.": ".$x_value;<br/>
}

function get_headers_from_curl_response($response)
{<br/>
    $headers = array();

    $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));

    foreach (explode("\r\n", $header_text) as $i => $line)
        if ($i === 0)
            $headers['http_code'] = $line;
        else
        {
            list ($key, $value) = explode(': ', $line);

            $headers[$key] = $value;
        }

    return $headers;
}



The out put from this is like

HTTP/1.1 200 OK 
Date: Thu, 07 May 2015 03:26:26 GMT
Server: Apache/2.4.12 (Win32) OpenSSL/1.0.1l PHP/5.6.8
X-Powered-By: PHP/5.6.8
Content-Length: 128
Content-Type: text/html; charset=UTF-8

but I want to add some more to this like

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Path=/application_uri
Freeflow: FC
charge: Y
amount: 100
Expires: -1
Pragma: no-cache
Cache-Control: max-age=0
Content-Type: UTF-8
Content-Length: 20
4
  • Which headers exactly do you need to add? Commented May 7, 2015 at 3:42
  • I want to add these Path=/application_uri Freeflow: FC charge: Y amount: 100 but i don't know how to get Expires: -1 Pragma: no-cache Cache-Control: max-age=0 These as well Commented May 7, 2015 at 3:45
  • You posted the curl script that makes a request from the client side. The response headers are returned by the server side script response.php. We need to see response.php to help. Commented May 7, 2015 at 4:04
  • I don't have one I do have only HTTP request wich is a URL I want to extract that url and response. 127.0.0.1:8080/… I'm sorry I have no idea how to do this Commented May 7, 2015 at 4:10

1 Answer 1

2
$headers = array( 'Path: application_uri',
                  'Freeflow: FC',
                  'charge: Y',
                  'amount: 100',
                 );
curl_setopt ($ch, CURLOPT_HTTPHEADER,$headers);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.