dongtun3328 2016-10-03 11:28
浏览 79
已采纳

如何将命令行cURL转换为PHP cURL?

I have command

curl -v -X POST -H "Content-Type: application/xml" -d @case.xml --user test:test url

in php send

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($ch, CURLOPT_USERPWD, "test:test");
curl_setopt($ch, CURLOPT_POSTFIELDS, ['data' => new CurlFile('case.xml')]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

but this code not work, error 400 Bad Request.

The web server uses Basic HTTP Authentication. I'm using 7.0.11

  • 写回答

1条回答 默认 最新

  • doumin1897 2016-10-04 13:40
    关注
    <?php
    $login='test';
    $password='test';
    $xml_data =file_get_contents(realpath('case.xml'));
    $url ='mega_url';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_HEADER,true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
    curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
    $output = curl_exec($ch);
    

    it is work

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?