【问题标题】:Unable to pull data from API using file_get_contents()无法使用 file_get_contents() 从 API 中提取数据
【发布时间】:2019-04-17 07:57:55
【问题描述】:

我正在尝试从 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);

【问题讨论】:

  • 尝试传递一个有效的用户代理。一些 API 拒绝没有它的连接。
  • file_get_content 是否与其他 url 一起使用?你考虑过使用 curl 吗?
  • @R.Martin 我已经改变了调用 API 的方式,现在 cURL 但没有返回响应是空的。这段代码从 POST cURL 发送请求是否正确
  • 似乎正确,您可以添加 CURLOPT_SSL_VERIFYPEER 选项

标签: php api


【解决方案1】:

使用 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);

【讨论】:

    猜你喜欢
    • 2020-11-03
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    相关资源
    最近更新 更多