2017-09-23 244 views
0

如何在不使用CURL的情況下刪除API調用cloudflare?使用PHP調用CloudFlare但不使用Curl

我的託管服務提供商不提供我很難爲情服務


我特別感興趣,並想使PHP API調用的CloudFlare從緩存中清除所有文件。

在API頁面,我發現

enter image description here

然後做了研究,研究還研究並通過這樣做

<?php 
$data = array (
    "purge_everything" => true 
); 
$url = "https://api.cloudflare.com/client/v4/zones/MYZONEID/purge_cache"; 
$opts = array('http' => 
        array(
         'method' => 'DELETE', 
         'header' => "Content-Type: application/json\r\n" . "X-Auth-Key: MYKEY\r\n" . "X-Auth-Email: MYEMAIL\r\n", 
         'data' => json_encode($data) 
       ) 
); 
$context = stream_context_create($opts); 
$fp = @fopen($url, 'rb', false, $context); 
if (!$fp) { 
    throw new Exception("Problem with $url, $php_errormsg"); 
} 
$response = @stream_get_contents($fp); 
if ($response === false) { 
    throw new Exception("Problem reading data from $url, $php_errormsg"); 
} 
return $response; 

找到了一種方法(也許)但在這個

Fatal error: Uncaught Exception: Problem with https://api.cloudflare.com/client/v4/zones/MYZONEID/purge_cache, in /srv/disk11/2444530/www/xxxx.pl/test.php:16 Stack trace: #0 {main} thrown in /srv/disk11/2444530/www/xxxx.pl/test.php on line 16 
遇到錯誤

我也試過這種方式:

<?php 
$data = array (
    "purge_everything" => true 
); 
$method = "getCallDetails"; 
$url = "https://api.cloudflare.com/client/v4/zones/MYZONEID/purge_cache"; 
$opts = array('http' => 
        array(
         'method' => 'DELETE', 
         'header' => "Content-Type: application/json\r\n" . "X-Auth-Key: MyKEY\r\n" . "X-Auth-Email: myEMAIL\r\n", 
         'data' => json_encode($data) 
       ) 
); 
$context = stream_context_create($opts); 
$result = file_get_contents($url, false, $context); 

return $result; 

還不錯的錯誤,這個時候:

Warning: file_get_contents(https://api.cloudflare.com/client/v4/zones/MYAPIID/purge_cache): failed to open stream: Network is unreachable in /srv/disk11/2444530/www/xxxx.pl/test.php on line 15 

當我得到瀏覽器到我的https://api.cloudflare.com/client/v4/zones/MYAPIID/purge_cache我看到

enter image description here

可達但不工作,東西在這裏很糟糕,甚至根本不可能。

那麼它是否可以使這件事情沒有CURL?如果是的話,該怎麼做?

+0

有什麼不對實際使用捲曲? – BenM

+0

@BenM我的託管服務不提供給我Curl –

+1

如何使用非垃圾提供程序呢?我可以想象沒有SSH訪問,但沒有提供基本擴展之一? –

回答

1

PHP HTTP包裝不是Curl,因此您需要將--data從Curl命令行切換轉換爲PHP中適當的HTTP上下文選項。

PHP中的條目是「content」。應該用它來代替「data」。

例子:

$authKey = "MyKEY"; 
$authEmail = "myEMAIL"; 

$zoneId = "MYZONEID"; 
$endpoint = "purge_cache"; 

$data = [ 
    "purge_everything" => true 
]; 

$url = "https://api.cloudflare.com/client/v4/zones/{$zoneId}/{$endpoint}"; 
$opts = ['http' => [ 
    'method' => 'DELETE', 
    'header' => [ 
     "Content-Type: application/json", 
     "X-Auth-Key: {$authKey}", 
     "X-Auth-Email: {$authEmail}", 
    ], 
    'content' => json_encode($data), 
]]; 
$context = stream_context_create($opts); 
$result = file_get_contents($url, false, $context); 

# [...] parse response 

看得那麼清楚:

-2

看起來像一些防火牆阻止file_get_contents連接到外部網絡。你最後的代碼,file_get_contents,應該已經工作。聯繫您的虛擬主機客戶支持,並要求他們解除您的PHP腳本訪問網絡。哦,並讓他們添加curl支持,或者如果他們真的不能這樣做,他們是一個低劣的虛擬主機,你應該找到一個新的。

+0

嘗試從我的xampp的本地主機相同的腳本,同樣的事情(錯誤) –