代码之家  ›  专栏  ›  技术社区  ›  Paulo Coghi

php:如何用XML数据发送REST请求

  •  2
  • Paulo Coghi  · 技术社区  · 14 年前

    我需要使用PHP中的rest(post方法)向API发出请求。

    但是数据必须是XML格式。如何用XML数据发送REST请求?

    谢谢您!

    2 回复  |  直到 14 年前
        1
  •  4
  •   Paulo Coghi    14 年前

    我用的是“fopen”,它起作用了。

    //You can use 'POST','GET','PUT' or 'DELETE'
    $opts = array(
        'http'=>array(
            'method'=>'POST',
            'header'=>"Content-Type: text/xml\r\n" .
                $auth."\r\n",
            'content'=>$xml
        )
    );
    
    $context = stream_context_create($opts);
    
    /* Sends an http request to www.example.com
    with additional headers shown above */
    $fp = fopen($url, 'r', false, $context);
    fpassthru($fp);
    fclose($fp);
        2
  •  0
  •   Jason McCreary    14 年前

    curl

    这可用于精细设置多个收割台- POST , PUT , DELETE -对于您来说,除了发送一个有效负载之外,还可以休息请求——XML内容。