【问题标题】:how to get response of http request如何获得http请求的响应
【发布时间】:2018-03-30 10:22:39
【问题描述】:

[![在此处输入图像描述][1]][1]我正在尝试获取某个标记值,但它显示了一些错误。 以下是代码,请提出一些解决方案。

这是我用于 httpGet 请求的方法。

function httpGet($result15)
{
$ch = curl_init();  

curl_setopt($ch,CURLOPT_URL,$result15);

curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

$output=curl_exec($ch);

curl_close($ch);

return $output;
}



$result15= httpGet("https://www.googleapis.com/customsearch/v1?key=API_KEY&cx=003255er&q=cancer&num=1&alt=atom");//new cse

echo $result15;
$xml = new DOMDocument();
$xml->loadXML($result15);
foreach( $xml->entry as $entry )
{
echo "URL=".(string)$entry->id.PHP_EOL;
echo "Summary=".(string)$entry->summary.PHP_EOL;

}

【问题讨论】:

  • 粘贴此错误
  • 我已经上传了错误信息截图,请查看@prgj83
  • 您的方法 httpGet 不返回任何值。这个方法怎么样?
  • 函数 httpGet($result15) { $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$result15); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); $output=curl_exec($ch); curl_close($ch);返回$输出; }
  • 我在问题@prgj83中添加了httpGet方法

标签: php xml google-custom-search


【解决方案1】:

您可能会发现 curl 请求失败。你需要做几件事......

function httpGet($result15)
{
    $ch = curl_init();  
    curl_setopt($ch,CURLOPT_URL,$result15);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Add this
    $output=curl_exec($ch);
    // If this fails, output error.
    if ($output === FALSE) {
        echo curl_error($ch);
        // Not sure what you want to do, but 'exit' will work for now
        exit();
    }
    curl_close($ch);
    return $output;
}

如果 curl 请求失败,这将显示错误。您将需要决定如何应对这种情况。您可以返回 false,然后在您的代码中进一步检查,然后再尝试将其加载为 XML。上面的代码只是在出错时停止。

您的下一段代码似乎混合了 SimpleXML 和 DOMDocument,如果文档结构相当简单,您可以使用 SimpleXML...

$xml = simplexml_load_string($result15);
foreach( $xml->entry as $entry )
{

【讨论】:

  • 感谢您的回复,现在我收到此错误通知:未定义的属性:C:\wamp64\www\hari\cfp\www.checkforplag.com-final-new 中的 DOMDocument::$entry \custom-search-api-atom.php 第 46 行
  • echo $result show: onlinelibrary.wiley.com/journal/10970142 不依从结直肠癌症 (CRC) 筛查在亚裔
    美国人中仍然很高。非依从性的菲律宾、苗族和韩裔美国人
    的筛查意愿较低。了解一项或多项筛查指南
    会使这些亚洲人
    群体中有意进行 CRC 筛查的几率增加一倍。
  • 好的,我在答案中添加了一点新内容,尝试使用 SimpleXML。
猜你喜欢
  • 1970-01-01
  • 2015-12-07
  • 1970-01-01
  • 2012-12-19
  • 2011-12-21
  • 2016-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多