2016-09-06 4 views
0

다음 코드는 명령 행에서 잘 작동합니다.PHP에서 컬 변경

curl -F customerid=1 -F username=admin -F password=admin -F deviceid=ID5500 -F [email protected]/var/www/html/test/sample.xml http://192.168.100.27:8080/trafficinsight/api/v1/setdataxml 

지금, 나는이 PHP 코드를 변경하려면, 내 코드는 다음과 같습니다 :

<data> 
<datum time="2016-09-02T16:00" c1="1" c2="1" c3="1" c4="1"/> 
<datum time="2016-09-02T17:00" c1="21" c2="1" c4="1"/> 
<datum time="2016-09-02T18:00" c2="27"/> 
<datum time="2016-09-02T16:00" c1="12" c2="21" c3="21" c4="7"/> 
<datum time="2016-09-02T19:00" c1="29"/> 
</data> 

하지만 지금 일하고, 나는 점점 오전 :

<?php 
     $fields = array 
     (
      'customerid' =>1, 
      'username'   =>'admin', 
      'password'   =>'admin', 
      'deviceid'   =>'ID5500', 
      'values'   =>'@/var/www/html/test/sample.xml', 
      'start'   =>'2016-05-24T16:00', 
      'end'   =>'2016-05-24T17:00' 
     ); 
     $ch = curl_init(); 
     curl_setopt($ch,CURLOPT_URL, 'http://192.168.100.27:8080/trafficinsight/api/v1/setdataxml'); 
     curl_setopt($ch,CURLOPT_POST, true); 
     curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($fields)); 
     $result = curl_exec($ch); 
     curl_close($ch); 

    echo '<pre>'; print_r($result); 


    die; 


?> 

내 XML 데이터입니다 오류.

[반응] => ERROR [응답 코드] =>

+0

요청의 수신자가 게시 필드를 json으로 예상 할 수 있습니까, 아니면 필드 중 하나가 다른 형식이어야합니까? –

답변

2

400 단지 CURLOPT_POSTFIELDS 배열로 배열주고 http_build_query()를 사용하지 않는다. 파일 업로드는 URL로 인코딩 된 문자열에 넣을 수 없습니다.

curl_setopt($ch,CURLOPT_POSTFIELDS, $fields); 

또한 게시 필드에서 @의 사용은 PHP 5.5에서 더 이상 사용되지 않습니다. 5.5.0 이상을 사용하는 경우 CURLFile 클래스를 사용해야합니다.

$fields = array 
    (
     'customerid' =>1, 
     'username'  =>'admin', 
     'password'  =>'admin', 
     'deviceid'  =>'ID5500', 
     'values'  => new CURLFile('/var/www/html/test/sample.xml', 'text/xml', 'sample.xml'), 
     'start'   =>'2016-05-24T16:00', 
     'end'   =>'2016-05-24T17:00' 
    );