开发者

php make post request with header

开发者 https://www.devze.com 2023-04-09 09:09 出处:网络
Using php, i need 开发者_开发问答to call a webservice, passing in specific data in the HTTP header, and specific data in the actual POST string.However, when ever i access this page, the page attempts

Using php, i need 开发者_开发问答to call a webservice, passing in specific data in the HTTP header, and specific data in the actual POST string. However, when ever i access this page, the page attempts to download. If I open the downloaded file, it contains only my two !'s. From what I understand, a response should be sent back.

Am I correctly "sending" the headers to the url defined below, or should i be doing it differently?

Thanks!

<?php 
    header("Content-Type: application/x-www-form-urlencoded");
    header("Content-Length: ---");
    header("Authorization: Basic -------");

try{

    $xml = "------";

    $url = 'https://---.com/---/---?site_id=---&service_name=---';

    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_POST, 1);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);

    curl_close($ch);

    echo "!" . $response . "!";

} catch (Exception $e) {
    print_r($e);
}

?>


No. The PHP header() call will issue headers that affect the connection between the web server and YOUR browser. it does not in any way affect the connection the server is creating with curl.

By default, curl will automatically fill in the appropriate content-type headers when you tell it to do a POST, and is smart enough to change the content type if you're doing a file upload as well. To specify the login credentials, use CURLOPT_USERPWD (see http://php.net/curl_setopt for details).


It looks like it's a https issue which might be solved through:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Cheers!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号