在PHP中使用curl创建PUT调用

时间:2016-08-08 10:09:11

标签: php rest curl put

我正在尝试用PHP创建一个PUT调用。

当我在Postman制作所有标题等时,一切正常。但是当我尝试在PHP中创建一个curl调用时,我会超时。

邮递员的原始代码:

PUT /rest/Google/save_google_token HTTP/1.1
Host: mySite.cz
Content-Type: application/json
OAuth-token: bfb33249-4f9a-a89f-38aa-57a8487a6848
Cache-Control: no-cache
Postman-Token: bf2f9320-01e7-1e14-b431-657b5e8d58b5

{
    "auth" : "4/NSzqsmIJabQaiwva1L7L1RBzK4M9yeRJuU9ScG24r5o"
}

我在PHP中的尝试:

$urlApi = "http://mySite.cz/rest/Google/save_google_token";
 $auth = $_GET['code'];
 $token =  $_GET['state'];

if (isset($auth) && isset($token)) { // we received the positive auth callback, get the token and store it

    $data = array(
        'auth' => $auth,
    );
    $data = json_encode($data);

    $curl = curl_init($urlApi);

    //for some reason, these queries do not work
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json',"OAuth-Token: $token"));
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    $response = curl_exec($curl);

    if (!$response) {
        die("Connection Failure.");
    }
}

1 个答案:

答案 0 :(得分:1)

事实证明,我的服务器阻止了自己的呼叫。 我刚刚改变了

"http://mySite.cz/rest/Google/save_google_token"

"https://127.0.0.1/rest/Google/save_google_token"

并添加了

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

这解决了这个问题。