Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使类方法在foreach循环-php内工作_Php_Oop_Curl_Feedparser - Fatal编程技术网

无法使类方法在foreach循环-php内工作

无法使类方法在foreach循环-php内工作,php,oop,curl,feedparser,Php,Oop,Curl,Feedparser,我编写了一个类链接,它有一个shortTolong()方法,它应该通过返回“location”响应头来返回缩短的URL的真实URL。我测试了它,它工作正常 这是密码 public function shortTolong() { $urlMatch = array(); $ch = curl_init(); $options = array ( CURLOPT_URL=>$this->g

我编写了一个类链接,它有一个shortTolong()方法,它应该通过返回“location”响应头来返回缩短的URL的真实URL。我测试了它,它工作正常 这是密码

public function shortTolong()
    {
        $urlMatch = array();
        $ch = curl_init();

        $options = array
        (
            CURLOPT_URL=>$this->getUrl(),
            CURLOPT_HEADER=>true,
            CURLOPT_RETURNTRANSFER=>true,
            CURLOPT_FOLLOWLOCATION=>false,
            CURLOPT_NOBODY=>true);
        curl_setopt_array($ch, $options);
        $server_output = curl_exec($ch);
        preg_match_all(LINK, $server_output,&$urlMatch,PREG_SET_ORDER);
        if($urlMatch)
        {
            foreach($urlMatch as $set)
            {
                $extracted_url = $set[2].'://'.$set[3];
            }
            return $extracted_url;
        }
        else
        {
            return $this->getUrl();
        }
    }
当我尝试在另一个文件上使用此方法时,问题就开始了,该文件使用FeedParser获取包含此短url的提要条目,出于某种原因,我需要对这些短url进行分析,结果是短url而不是长url。代码如下:

foreach($parser->getItems() as $item)
{
    $idpreg = '/\d+/';
    preg_match_all($idpreg, $item['ID'],$statusid);
    $retweetid = ($statusid[0][1]);
    $datetime = $item['PUBLISHED'];
    $user = $item['AUTHOR']['NAME'];
    preg_match_all(LINK, $item['TITLE'], &$linkMatch);
    $final = $linkMatch[0][0];
    //if($linkMatch[0][0])
        echo '<p>';
        $link = new Link($final);
        echo $link->getUrl();
        echo '<br>';
        echo $link->shortTolong();
        echo '<br>';
        echo $user;
        echo '<br>';
        echo $retweetid;
        echo '</p>';


}
foreach($parser->getItems()作为$item)
{
$idpreg='/\d+/';
preg_match_all($idpreg,$item['ID'],$statusid);
$retweetid=($statusid[0][1]);
$datetime=$item['PUBLISHED'];
$user=$item['AUTHOR']['NAME'];
预匹配(链接、$item['TITLE']和$linkMatch);
$final=$linkMatch[0][0];
//如果($linkMatch[0][0])
回声“”;
$link=新链接($final);
echo$link->getUrl();
回声“
”; echo$link->shortTolong(); 回声“
”; echo$user; 回声“
”; echo$retweetid; 回声“

”; }
出于某种原因,我对getUrl()和shortTolong()得到了相同的结果,我确信这是一个错误

知道为什么会这样吗? 谢谢

编辑-我使用curl\u eror向方法添加了一个错误通知 我收到以下错误消息:“libcurl中不支持或禁用http协议”
正如我所说的,我从测试了这个方法,它在相同的环境中可以独立运行(没有变化),我怀疑它也与使用curl的FeedParser有关……

我认为你应该修剪()url,这应该可以解决问题。

你试过echo$server\u output=curl\u exec($ch)??这会打印一些输出吗?对url进行修剪,看看是否有帮助。sab-感谢您响应$server\u output按预期输出url的响应标题。您能解释一下为什么您认为修剪()会有帮助吗?正如我所说的,当我在静态生成的对象上测试它时,这个方法工作得很好。当使用FeedParsed动态启动对象时,问题就开始了。在我的回答注释中是这样的。。。ur edit“libcurl中不支持或禁用http协议”。。什么时候开始?如果在您从馈送循环请求时出现。。很可能url周围有一些你看不到的额外内容。。试试看。。否则。。没有理由像现在这样发生。。发送显式curl_close($ch)也很好;在请求完成后,我想我应该向你道歉:)虽然我对trim()的解决方案表示怀疑,但它的效果很好尊敬!