如何使php能够进行http调用
在尝试了一整晚但没有成功后,我的代码应该可以工作,但不能工作:如何使php能够进行http调用,php,Php,在尝试了一整晚但没有成功后,我的代码应该可以工作,但不能工作: <?php // 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 => 'http://api.keyn
<?php
// 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 => 'http://api.keynote.com/keynote/',
CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
if(!curl_exec($curl)){
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
// Close request to clear up some resources
curl_close($curl);
echo $resp;
?>
在服务器上,我可以使用任何浏览器手动打开此url,而不会出现任何问题
如何使php连接到internet?问题是,
fsockopen
用于打开socks(即连接到指定主机/IP上的指定端口)
当您尝试向主机“”打开sock时,就像运行“ping”-您将得到一个错误-因为没有这样的主机“http://”
你要做的是使用或curl
<?php
$response = http_get("http://www.example.com/", array("timeout"=>1), $info);
print_r($info);
?>
或删除“http://”
为什么不使用cURL?“找不到套接字传输“http”-您在配置PHP时忘记启用它了吗?”我非常喜欢PHP错误消息的清除程度are@Dagon,不太熟悉php,您是否愿意分享一下如何使用http配置php的经验?它是一个扩展,使用第二个代码或这里的一个示例:如果它是您的主机,您可能可以安装PECL:I尝试了curl,get contents。没有结果。看起来我无法在php内连接到Internet。当我把这个xll放到那个服务器上的浏览器URL上时,我可以手动获取它。我不知道该怎么办?在我的php.ini中,我看到allow_url_fopen=On仍然不起作用。这应该是非常直截了当的,因为许多其他语言很容易做到这一点。我花了这么多时间来搞清楚这件事。
<?php
$response = http_get("http://www.example.com/", array("timeout"=>1), $info);
print_r($info);
?>
<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>