2016-11-10 123 views
1

我將如何在PHP中使用此命令CURL提交此test.xml文件:將終端CURL轉換爲PHP CURL?

curl -X POST -k -d @test.xml -H "Content-Type: application/xml" --user username:password https://www.url.com 

這不是爲我工作:

$ch = curl_init(); 

//Get the response from cURL 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

//Set the Url 
curl_setopt($ch, CURLOPT_URL, 'https://username:[email protected]'); 

//Create a POST array with the file in it 
$postData = array(
'testData' => '@test.xml', 
); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 

// Execute the request 
$response = curl_exec($ch); 

我也想知道我怎麼能提交XML編寫代碼而不是XML文件。

+1

不把用戶名\密碼在url – 2016-11-10 20:52:23

+0

是唯一的錯誤在我的代碼?以及我將如何傳遞用戶名和密碼 – Omar

回答

1

您不應該在網址中輸入用戶名和密碼。對於$username$password,您可以使用curl_setopt($p, CURLOPT_USERPWD, $username . ":" . $password);。例如下面:

$p = curl_init($url); 
curl_setopt($p, CURLOPT_HTTPHEADER, array('Content-Type: application/xml')); 
curl_setopt($p, CURLOPT_HEADER, 1); 
curl_setopt($p, CURLOPT_USERPWD, $username . ":" . $password); 
curl_setopt($p, CURLOPT_POSTFIELDS, $postData); 
curl_setopt($p, CURLOPT_POST, 1); 
curl_setopt($p, CURLOPT_RETURNTRANSFER, TRUE); 
$return = curl_exec($p); 
curl_close($p); 
+0

在您的代碼中,我將如何爲其發送XML? – Omar