PHP有两个与超时相关的选项:CURLOPT_CONNECTTIMEOUT和CURLOPT_TIMEOUT.
PHP站点上的描述有点模糊.有什么不同?
要使用真实世界的示例:假设您通过cURL将GET变量发送到URL并且您希望接收XML,则CURLOPT_CONNECTTIMEOUT与连接到服务器CURLOPT_TIMEOUT所花费的最长时间和最长时间有关可以将XML发回去吗?
这个网站现在已经运行了几个月,并且一直运行良好.我有一个PHP页面,它根据网址中的数据创建发票(例如,viewinvoice.php?id = 250根据记录250建立发票).此页面可通过Web浏览器访问,并且工作正常.
在一个完全不同的页面(即test.php)上,我试图通过cURL访问该文件.但是,当我进行调用并且var_dump结果时,我得到bool(false).
这是进行cURL调用的函数:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
Run Code Online (Sandbox Code Playgroud)
HOME是一个表示完整URL的常量(例如http://www.example.com/).
$invoice_contents = file_get_contents_curl(HOME.'viewinvoice.php?id=242');
echo $invoice_contents;
var_dump( $invoice_contents );
Run Code Online (Sandbox Code Playgroud)
我已经尝试将网址更改为外部网址(即http://www.google.com/),页面加载就好了.我得到谷歌的主页.但是同一域中的任何页面都不会加载.这有可能发生吗?
我不是服务器管理员,但我有权访问服务器.我最近没有更改任何设置,但服务器管理员可能已经升级了apache或php的版本?
在任何情况下,是否有我可以修改的设置以使其再次工作?
PS我刚尝试从外部服务器(不同的域)进行这个确切的调用,它工作得很好.
即使我将发送到我的浏览器的网址复制并粘贴,我也会一直获得0的状态,我得到了一个json对象
<?php
$mainUrl = "https://api.xxxx.com/?";
$co = "xxxxx";
$pa = "xxxx";
$par = "xxxx";
$part= "xxxx";
$partn = "xxxx";
$us= "xxx";
$fields_string;
$fields = array(
'co'=>urlencode($co),
'pa'=>urlencode($pa),
'par'=>urlencode($par),
'part'=>urlencode($part),
'partn'=>urlencode($partn),
'us'=>urlencode($us)
);
foreach($fields as $key=>$value) { $fields_string .= $key . '=' . $value . '&' ;}
$fields_string = rtrim($fields_string, "&");
$fields_string = "?" . $fields_string;
$url = "https://api.xxxxx.com/" . $fields_string;
$request = $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,'3');
$content = trim(curl_exec($ch));
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); …Run Code Online (Sandbox Code Playgroud) 这是交易.我要求监控某些受密码保护的网页以进行更改,并在页面大小与我所知(即已修改)不同时播放声音警报.这是我提出的一大堆代码.
<?php
$e = curl_init();
curl_setopt($e, CURLOPT_URL, 'http://example.com/Account/LogOn');
curl_setopt($e, CURLOPT_POST, 1);
curl_setopt($e, CURLOPT_POSTFIELDS, 'UserName=xxx@example.com&Password=password');
curl_setopt($e, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($e, CURLOPT_REFERER, 'http://example.com');
curl_setopt($e, CURLOPT_RETURNTRANSFER, 1);
curl_exec($e);
$sizevalue = 2399;
do {
curl_setopt($e, CURLOPT_URL, 'http://example.com/panel.php');
$content = curl_exec($e);
$numberofchars = strlen($content);
sleep(15);
} while ($numberofchars = $sizevalue);
curl_close($e);
echo '
<audio controls="controls" autoplay="autoplay">
<source src="/path/to/your/audio.mp3" type="audio/mp3">
<source src="/path/to/your/audio.ogg" type="audio/ogg">
<embed src="/path/to/your/audio.mp3">
</audio>';
php?>
Run Code Online (Sandbox Code Playgroud)
问题1.如果我错了,请纠正我,但据我所知,不是连接服务器,只发布一次用户名和密码,保持连接处于活动状态并使用该auth密钥cookie.txt进行后续操作,它会不断地将用户名和密码发送到每个登录表单.转动周期.我该如何解决?
问题2.我需要脚本给我一些临时状态,因为目前如果不满足某些条件它基本上变成无限循环,托管脚本的服务器给我超时错误.
问题3.实现声音警报的最简单方法是什么?OGG文件播放?Youtube重定向?
我想将PHP的特定部分限制为X秒 - 如果需要更长时间,则终止当前正在执行的代码(只是部分,而不是整个脚本)并运行备用代码.
伪代码示例(这里的示例用例是一个不稳定的API,有时很快,其他时候是黑洞):
$completed = 1;
$seconds = 60;
while ($completed != -1 && $completed < 5) {
limit ($seconds) {
$api = new SomeAPI('user','secret','key');
$data = $api->getStuff('user="bob"');
$completed = -1;
} catch () {
$completed++;
sleep(10);
}
}
if ($completed === 5) echo "Error: API black-hole'd 5 times.\n";
else {
//Notice: data processing is OUTSIDE of the time limit
foreach ($data as $row) {
echo $row['name'].': '.$row['message']."\n";
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这应该适用于任何事情.不只是API/HTTP请求.例如密集的数据库程序.
如果你读得太快: set_time_limit和max_execution_time …
我有一个curl put请求在我的localhost上工作正常,但在实时服务器上它会抛出500错误.这是我的代码:
public static function send( $xml )
{
$xml = str_replace( "\n", "", $xml );
//Write to temporary file
$put_data = tmpfile();
fwrite( $put_data, $xml );
fseek( $put_data, 0 );
$options = array(
CURLOPT_URL => 'http://*****************/cgi-bin/commctrl.pl?SessionId=' . Xml_helper::generate_session_id() . '&SystemId=live',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => array( 'Content-type: text/xml' ),
CURLOPT_PUT => TRUE,
CURLOPT_INFILE => $put_data,
CURLOPT_INFILESIZE => strlen( $xml )
);
$curl = curl_init();
curl_setopt_array( $curl, $options );
$result = curl_exec( $curl );
curl_close( $curl );
return $result;
} …Run Code Online (Sandbox Code Playgroud) 我们正在使用网络服务,但有时,他们没有回应,他们太长时间无法回应.
如果超过1秒钟,如何停止cURL?
我试过了 :
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
Run Code Online (Sandbox Code Playgroud)
我还试图在我的服务器和webservice之间创建一个"临时页面":我的服务器调用一个临时页面:
set_time_limit(1);
ini_set('max_execution_time', 1);
Run Code Online (Sandbox Code Playgroud)
这个临时页面用curl调用webservice本身,但仍然没有.如果我的web服务的执行时间为10秒,我将不得不等待10秒钟.
有任何想法吗 ?
$url='http://api.cruiseline.com/cruises/7-night-western-caribbean-ft-lauderdale-roundtrip-35052/detail';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$result=json_decode($result);
var_dump($result);
Run Code Online (Sandbox Code Playgroud)
这是curl_getinfo()的响应
array(26) { ["url"]=> string(96) "http://api.cruiseline.com/cruises/7-night-western-caribbean-ft-lauderdale-roundtrip-35052/detail" ["content_type"]=> string(16) "application/json" ["http_code"]=> int(200) ["header_size"]=> int(511) ["request_size"]=> int(127) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(5.11) ["namelookup_time"]=> float(0.203) ["connect_time"]=> float(0.532) ["pretransfer_time"]=> float(0.532) ["size_upload"]=> float(0) ["size_download"]=> float(650744) ["speed_download"]=> float(127347) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(0) ["starttransfer_time"]=> float(1.125) ["redirect_time"]=> float(0) ["certinfo"]=> array(0) { } ["primary_ip"]=> string(14) "75.101.141.196" ["primary_port"]=> int(80) ["local_ip"]=> string(13) "192.168.10.15" ["local_port"]=> int(59916) ["redirect_url"]=> string(0) "" }
Run Code Online (Sandbox Code Playgroud)
以上代码有时会发送完整的json数据,有时会发送部分数据到 …