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

如何将命令行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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题