当命令正常工作时,PHP Curl超时

当命令正常工作时,PHP Curl超时,php,curl,philips-hue,Php,Curl,Philips Hue,我希望将下面的cURL命令转换为PHP curl -X PUT -H 'Content-Type: application/json' -d '{"on":true,"bri":255,"sat":255,"hue":20000}' http://MYSITE:PORT/api/HASH/lights/1/state 我做了以下操作,但只是超时了 $data_json = '{"on":true,"bri":255,"sat":255,"hue":20000}'; $url = "http:/

我希望将下面的cURL命令转换为PHP

curl -X PUT -H 'Content-Type: application/json' -d '{"on":true,"bri":255,"sat":255,"hue":20000}' http://MYSITE:PORT/api/HASH/lights/1/state
我做了以下操作,但只是超时了

$data_json = '{"on":true,"bri":255,"sat":255,"hue":20000}';
$url = "http://MYSITE:PORT/api/HASH/lights/1/state";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);

$response  = curl_exec($ch);
echo $response;
curl_close($ch);

我将尝试使用前面提到的技术调试Curl请求,这将为您提供有关Curl正在做什么的更多信息

此外,如果您愿意使用我推荐使用的库,它有很好的文档记录,并且根据我的经验,可以减少此类操作的痛苦

<?php

use GuzzleHttp\Client;

require 'vendor/autoload.php';

$url = "http://jsonplaceholder.typicode.com/posts";
$client = new Client();

$body['title'] = "json guzzle post";
$body['body'] = "carlton";
$body['userId'] = "109109101";

$res = $client->post($url, [ 'body' => json_encode($body) ]);

$code = $res->getStatusCode();
$result = $res->json();

var_dump($code);
var_dump($result);

您可以这样设置卷曲超时

curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,0); curl_setopt($ch,CURLOPT_超时,400)//超时(秒)


您可能也想增加php.ini中的php超时时间

我遇到的问题是curl命令工作正常。但当我将其转换为PHP时,它并不像预期的那样工作。我的转换有问题吗?问题解决了。我的主机提供商阻止了对端口8078的请求。