所以我有这个PHP脚本处理上传到多个站点。总共有三个站点,现在他们使用相同的类进行上传但具有不同的API数组..
以下是处理上传的脚本之一...
class APIUpload
{
private $data;
public function __construct( Array $data )
{
$this -> data = $data;
}
public function doUpload()
{
$ch = curl_init();
if ( is_resource( $ch ) )
{
curl_setopt( $ch, CURLOPT_URL, 'http://example.com/upload_api/' );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $this -> data );
curl_setopt( $ch, CURLOPT_TIMEOUT, 5 );
$data = curl_exec( $ch );
curl_close( $ch );
}
if ( empty( $data ) )
{
$this -> error = 'An unexpected error occured.';
return;
}
return $this -> parse( $data );
}
private function parse( $data )
{
if ( ! ( $json = json_decode( $data, true ) ) Or ( json_last_error() ) )
{
$this -> error = 'There was a issue decoding the response.';
return;
}
if ( isset( $json -> error ) )
{
$this -> error = $json -> error;
return;
}
return $json;
}
public function getError()
{
return $this -> error;
}
}
$message = null;
if ( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' )
{
/* Initialize Upload */
$Api = new APIUpload([
'name' => $_POST[ 'name' ],
'category' => $_POST[ 'category' ],
'hash' => $_POST[ 'apikey' ],
'file' => '@' . $_POST[ 'file' ],
'description' => $_POST[ 'descr' ]
]);
if ( ! ( $data = $Api -> doUpload() ) )
{
$message = $Api -> getError();
}
else
{
$message = sprintf( 'Your file has been uploaded to: %s', $data[ 'ok' ] );
}
}
?>
在上面的php脚本下面,我有这个html脚本,它将输入的值发布到PHP字段....
<div style="color: #ff0000;"><?php echo $message; ?></div>
<form method="post" action="">
<fieldset>
<legend>API Example</legend>
<label for="name">Name</label>
<input type="text" name="name" id="name">
<label for="file">File</label>
<select name="file" id="file">
<option value="">Select File</option>
<?php foreach ( glob( 'files/*.torrent' ) As $file ): ?>
<option value="<?php echo $file; ?>"><?php echo basename( $file ); ?></option>
<?php endforeach; ?>
<label for="category">Category</label>
<select name="category" id="category">
<option value="0">Select A Category</option>
<option value="50">Adult XXX</option>
<option value="28">Anime</option>
<option value="18">Apps</option>
<option value="51">Books</option>
<option value="10">Games</option>
<option value="52">Mobile</option>
<option value="1">Movies</option>
<option value="22">Music</option>
<option value="33">Other</option>
<option value="70">Pictures</option>
<option value="113">TV</option>
<option value="71">Videos</option>
</select>
<label for="apikey">Apikey</label>
<input type="text" name="apikey" id="apikey" value= "">
<label for="description">Description</label>
<input type="text" name="descr" id="description" value="Default description...">
<input type="submit" value="Upload">
</fieldset>
</form>
所以我的问题是我有大约三个这样的PHP脚本要上传到其他网站,但我怎么能运行这个脚本,以便它可以立即运行其他...
所以,例如..我有以下
site1.php site2.php site3.php
主脚本有html表单可以一次运行...
...谢谢
答案 0 :(得分:0)
你必须使用多卷曲来同时运行多个请求,使用类似的东西。
$urls = array($site1, $site2, $site3);
$url_count = count($urls);
$curl_arr = array();
$master = curl_multi_init();
for($i = 0; $i < $url_count; $i++)
{
$url =$urls[$i];
$curl_arr[$i] = curl_init($url);
curl_setopt( $curl_arr[$i], CURLOPT_URL, 'http://example.com/upload_api/' );
curl_setopt( $curl_arr[$i], CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl_arr[$i], CURLOPT_POSTFIELDS, $this -> data );
curl_setopt( $curl_arr[$i], CURLOPT_TIMEOUT, 5 );
curl_multi_add_handle($master, $curl_arr[$i]);
}
do {
curl_multi_exec($master,$running);
} while($running > 0);