我正在尝试访问从游戏服务器托管的REST API。使用JQuery,我遇到了CORS安全性,所以我决定尝试使用PHP。尝试通过php请求数据返回null或超时响应。代码和回复如下所示。有什么想法吗?
<?php
$service_url = 'http://104.250.130.74:49025';
$ch = curl_init($service_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'POST /status.json?apikey=785kjgdl[stripped]rwD1T HTTP/1.1',
'Host: 104.250.130.74:49025',
'Content-Type: application/json',
'Accept: */*'
));
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).DIRECTORY_SEPERATOR.'cookie.txt');
$curl_response = curl_exec($ch);
if ($curl_response === false) {
$info = curl_getinfo($ch);
curl_close($ch);
echo 'Curl error: ' . curl_error($ch);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($ch);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_export($decoded->response);
?>
,响应如下。一些讨厌的'调试'文本,但我现在已经在这三天了:P
Curl error: array ( 'url' => 'http://104.250.130.74:49025/', 'content_type' => NULL, 'http_code' => 0, 'header_size' => 0, 'request_size' => 0, 'filetime' => -1, 'ssl_verify_result' => 0, 'redirect_count' => 0, 'total_time' => 62.260399, 'namelookup_time' => 0.000145, 'connect_time' => 0, 'pretransfer_time' => 0, 'size_upload' => 0, 'size_download' => 0, 'speed_download' => 0, 'speed_upload' => 0, 'download_content_length' => -1, 'upload_content_length' => -1, 'starttransfer_time' => 0, 'redirect_time' => 0, 'certinfo' => array ( ), 'redirect_url' => '', )error occured during curl exec.