我正在尝试从API提取数据,并收到此错误。我搜索了相同的file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known 但不知道如何解决
file_get_contents():php_network_getaddresses:getaddrinfo失败: 名称或服务未知
这是我的代码
<?php namespace AdClass;
use stdClass;
class AdInfo{
private $domain;
private $widget_id;
private $api_key;
private $pub_id;
public function __construct($domain, $widget_id, $api_key, $pub_id)
{
$this->domain = $domain;
$this->widget_id = $widget_id;
$this->api_key = $api_key;
$this->pub_id = $pub_id;
}
public function getDomain()
{
return $this->domain;
}
public function getWidgetId()
{
return $this->widget_id;
}
public function getApiKey()
{
return $this->api_key;
}
public function getPubId()
{
return $this->pub_id;
}
public function getAdContent()
{
// 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 => 'https://api.revcontent.com/api/v1',
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'api_key' => $this->api_key,
'pub_id' => $this->pub_id,
'widget_id' => $this->widget_id,
'domain' => $this->domain,
'format' => 'json',
'sponsored_offset' => '0',
'internal_count' => '3',
'internal_offset' => '2'
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
return $resp;
}
}
?>
index.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include(dirname(__FILE__).'/AdClass/AdInfo.php');
$domain = "realtimepolitics.com";
$widget_id = XXXX;
$api_key = "XXXXXX";
$pub_id = XXXX;
$adobj = new AdClass\AdInfo($domain, $widget_id, $api_key, $pub_id);
$response = $adobj->getAdContent();
echo "<pre>";
print_r($response);
答案 0 :(得分:0)
问题已通过cURL请求解决:
<?php namespace AdClass;
use stdClass;
class AdInfo{
private $domain;
private $widget_id;
private $api_key;
private $pub_id;
private $sponsored_offset;
private $sponsored_count;
private $internal_offset;
private $internal_count;
public function __construct($domain,$widget_id,$sponsored_count=1,$internal_offset=1, $sponsored_offset=0,$internal_count=3)
{
$this->domain = $domain;
$this->widget_id = $widget_id;
$this->api_key = "XXXX";
$this->pub_id = xxxx;
$this->sponsored_offset =$sponsored_offset;
$this->sponsored_count = $sponsored_count;
$this->internal_offset = $internal_offset;
$this->internal_count = $internal_count;
}
public function getAdContent()
{
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://trends.revcontent.com/api/v1/?api_key='.$this->api_key.'&pub_id='.$this->pub_id.'&widget_id='.$this->widget_id.'&domain='.$this->domain.'&format=json&sponsored_count='.$this->sponsored_count.'&sponsored_offset='.$this->sponsored_offset.'&internal_count='.$this->internal_count.'&internal_offset='.$this->internal_offset ,
CURLOPT_USERAGENT => 'cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
return $resp;
}
}
?>
Main.php
<?php namespace MainApi;
include(dirname(__FILE__).'/../AdClass/AdInfo.php');
use AdClass\AdInfo;
class Main{
private $adObj;
public function __construct($domain,$widget_id,$sponsored_count,$internal_offset, $sponsored_offset,$internal_count)
{
$this->domain = $domain;
$this->widget_id = $widget_id;
$this->sponsored_offset =$sponsored_offset;
$this->sponsored_count = $sponsored_count;
$this->internal_offset = $internal_offset;
$this->internal_count = $internal_count;
$this->adObj = new AdInfo($this->domain,$this->widget_id,$this->sponsored_count,$this->internal_offset, $this->sponsored_offset,$this->internal_count);
}
function getResponse()
{
$response = $this->adObj->getAdContent();
return $response;
}
}
getAds.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include (dirname(__FILE__) . '/MainApi/Main.php');
$domain = "realtimepolitics.com";
$widget_id = $_GET['widget_id']; //97862
$weight = $_GET['w'];
$height = $_GET['h'];
$sponsored_count = 2;
$internal_offset = 2;
$sponsored_offset = 0;
$internal_count = 3;
$main = new MainApi\Main($domain, $widget_id, $sponsored_count, $internal_offset, $sponsored_offset, $internal_count);
$response = $main->getResponse();
$ads_data = json_decode($response);
print_r($ads_data);