2017-02-24 59 views
1

我試圖通過curl調用一個來自php的url,但是沒有來自服務器的響應。如何從PHP執行curl url

以下是我的網址:

$url = 'https://id:[email protected]/oauth/token \ -X POST \ -d "grant_type=client_credentials&scope=manage_project:test"'; 

捲曲代碼

$auth = curl_init($url); 
curl_setopt($auth, CURLOPT_POST, true); 
curl_setopt($auth, CURLOPT_HEADER, false); 
curl_setopt($auth, CURLOPT_RETURNTRANSFER, true); 
$response = curl_exec($auth); 
curl_close($auth); 
print_r($response); 

當我執行從服務器終點站下車響應相同的URL。捲曲代碼有什麼問題。請幫忙。

+1

URL看起來不正確=好像是捲曲的命令行選項 –

+0

'CURL'安裝在您的Web服務器中,對不對? –

+0

是的,捲曲安裝 – Gowri

回答

1

使用文檔 - 這應該工作。注意正確的URL和正確地傳遞參數後

$auth = curl_init("https://auth.sphere.io/oauth/token"); 
curl_setopt($auth, CURLOPT_POST, true); 
curl_setopt($auth, CURLOPT_HEADER, false); 
curl_setopt($auth, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($auth,CURLOPT_POST, 2); 
curl_setopt($auth,CURLOPT_USERPWD, "id:secret"); 
curl_setopt($auth,CURLOPT_POSTFIELDS, "grant_type=client_credentials&scope=manage_project:test"); 
$response = curl_exec($auth); 
curl_close($auth); 
print_r($response); 
+0

試過。獲取缺少必需參數:grant_type'錯誤。 – Gowri

+0

這是來自服務器的響應嗎? –

+0

是從服務器。 – Gowri

3

檢查捲曲片段

$auth = curl_init("https://id:[email protected]/oauth/token"); 
    curl_setopt($auth, CURLOPT_POST, true); 
    curl_setopt($auth, CURLOPT_HEADER, false); 
    curl_setopt($auth, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($auth,CURLOPT_POST, 2); 
    curl_setopt($auth,CURLOPT_POSTFIELDS, "grant_type=client_credentials&scope=manage_project:test"); 
    $response = curl_exec($auth); 
    curl_close($auth); 
    print_r($response); 

提供正確的憑據,以便驗證成功

1

@Ed治癒 - 修改您的代碼以下應該工作

$auth = curl_init("https://auth.sphere.io/oauth/token"); 
curl_setopt($auth, CURLOPT_POST, true); 
curl_setopt($auth, CURLOPT_HEADER, false); 
curl_setopt($auth, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($auth,CURLOPT_POST, 2); 
curl_setopt($auth,CURLOPT_USERPWD, "id:secret"); 
curl_setopt($auth,CURLOPT_POSTFIELDS, "grant_type=client_credentials&scope=manage_project:test"); 
$response = curl_exec($auth); 
curl_close($auth); 
print_r($response); 

$ ch是問題,因爲捲曲我s初始化爲$ auth