Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alexever's avatar

Curl problem with string and variable?

Guys, I 'v need Curl to post xml to provider's server.
Would you please tell me what is the difference between those codes.

  1. Works fine.
    $xml='xml=US';
    $ch = curl_init();
    $header=[ "Content-type: application/x-www-form-urlencoded","charset=utf-8"];
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    curl_exec($ch);

    2.Fail.
    make a txt file contain the characters 'xml=US'.

    $xml=file_get_contents("country.txt");
    $ch = curl_init();
    $header=[ "Content-type: application/x-www-form-urlencoded","charset=utf-8"];
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    curl_exec($ch);

    So. what's the difference between $xml one set as charectors and another by file_get_contents?Thank you.
0 likes
0 replies

Please or to participate in this conversation.