Heim > Fragen und Antworten > Hauptteil
$url = 'http://xxx.xxx.xxx.xxx:x/xxx';
$ch = curl_init($url);
$payload = array(
'message' => 'notification with php curl'
);
$jsonDataEncoded = json_encode($payload);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($ch);
curl_close($ch);
我用了這個方式想用php 丟 json 到我指定的url...
只是怎麼試都無法運行
就會loading很久,到最後就是空白頁(而server端什麼事也沒發生)
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://testcURL.com',
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
item1 => 'value',
item2 => 'value2'
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
$r = new HttpRequest("http://xxx:xxx/", HttpRequest::METH_POST);
$r->setOptions(array("message" => "123"));
try {
echo $r->send()->getBody();
} catch (HttpException $ex) {
echo $ex;
}
試了很多種都還是失敗.....
黄舟2017-04-11 09:50:06
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://testcURL.com',
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
item1 => 'value',
item2 => 'value2'
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
PHPz2017-04-11 09:50:06
之前也遇到过这样的问题,共勉
class RestClient
{
public function get($url, $parameters = array(), $headers = array())
{
return $this->execute($url, 'GET', $parameters, $headers);
}
public function post($url, $parameters = array(), $headers = array())
{
return $this->execute($url, 'POST', $parameters, $headers);
}
public function put($url, $parameters = array(), $headers = array())
{
return $this->execute($url, 'PUT', $parameters, $headers);
}
public function delete($url, $parameters = array(), $headers = array())
{
return $this->execute($url, 'DELETE', $parameters, $headers);
}
public function execute($url, $method = 'GET', $parameters = array(), $headers = array())
{
$ch = curl_init();
$data_json = json_encode($parameters);
curl_setopt($ch, CURLOPT_URL, $url);
$upper_method = strtoupper($method);
if ($upper_method == 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
} else if ($upper_method != 'GET') {
curl_setopt($ch, CURLOPT_HTTPGET, true);
} else if ($upper_method != 'PUT') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
} else if ($upper_method != 'DELETE') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
}
if ($headers === NULL) {
curl_setopt($ch,
CURLOPT_HTTPHEADER,
array('Content-Type: application/json',
'Content-Length: ' . strlen($data_json)));
} else {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
阿神2017-04-11 09:50:06
代码都没错的话,题主可以检查几个地方:
请求的参数有没有问题,缺了什么或者少了什么,数据的格式等
请求的url那里有没有加权限验证
请求的服务器是否正常使用
再一个就是检查一下curl服务有没有开启(如果有开启的话请忽视这条
)
如果可以的话,题主可以贴一下服务端代码吗?