2014-05-14 23 views
0

首先,IAM與PHP和捲曲如何從cURL命令中提取數據/結果以顯示PHP輸出?

從命令頗有新意:

curl -i 'http://xxx:xxx/v2.0/tokens' -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{"auth": {"tenantName": "xxx", "passwordCredentials": {"username": "admin", "password": "xxx"}}}' 

我想用POST/GET和PHP產生這種命令的結果。我有一個Apache服務器和工作網址。

我只是不知道如何製作一個工作代碼,它將代碼和輸出生成到使用PHP的空白頁面。

我一直在尋找的例子,我知道你一定要使用以下一些:

<?php 
$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, ""); 
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, ''); 
curl_setopt($ch, CURLOPT_POSTFIELDS, ''); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

curl_exec($ch); 

curl_close($ch); 
?> 

我只是不知道如何正確的標誌來自卷邊命令連接到PHP代碼,可能錯過了一些重要的事情,幷包裝了代碼。

如果我無法解釋我的問題,請提前致謝,並對不起。

** ***編輯:* ****

所以我做了一個代碼,這適合我的需求,它產生從PHP頁面捲曲命令原始結果。 下面是代碼:

<?php 
$request_body = '{"auth": {"tenantName": "xxx", "passwordCredentials": {"username": "admin", "password": "xxx"}}}'; 
$ch = curl_init('http://xxx:xxx/v2.0/tokens'); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $request_body); 
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
     curl_setopt($ch, CURLOPT_HEADER, true); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
     $response = curl_exec($ch); 
var_dump($response); 
?> 

問題,如果現在我可以操縱的輸出以更好的方式來顯示,現在它只是一個大的字符串,某種形式的JSON漂亮打印的將是驚人的。

回答

0

瞭解更多關於捲曲EXEC(http://www.php.net/manual/en/function.curl-exec.php

<?php 
    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL, ""); 
    curl_setopt($ch, CURLOPT_HEADER, true); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, ''); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, ''); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    $data = curl_exec($ch); 

    curl_close($ch); 
print $data; 

    ?> 
+1

謝謝你的回覆。 – user3636061

相關問題